--******************************************************************
--                              Getter                              
--******************************************************************

function GetClosestTowerID( AILUATable )
	return ARMY.GetClosestID( AILUATable.myArmyID, "CAN_CAPTURE_TOWER" )
end

function GetClosestVisibleEnemyArmyID( AILUATable )
	return ARMY.GetClosestID( AILUATable.myArmyID, "VISIBLE_ENEMY_ARMY" )
end

function GetClosestCastleID( AILUATable )
	return ARMY.GetClosestID( AILUATable.myArmyID, "CAN_ATTACK_CASTLE" )
end

function GetTowerDif( Tower, AILUATable )
	local myPositionX, myPositionY = ARMY.GetPosition( AILUATable.myArmyID )
	local TowerPositionX, TowerPositionY = TOWER.GetPosition(Tower)
	return MAP.GetDistanceBetweenTwoPosition(myPositionX, myPositionY, TowerPositionX, TowerPositionY)
end

function GetEnemyDif( Enemy, AILUATable)
	local myPositionX, myPositionY = ARMY.GetPosition( AILUATable.myArmyID )
	local EnemyPositionX, EnemyPositionY = ARMY.GetPosition(Enemy)
	return MAP.GetDistanceBetweenTwoPosition(myPositionX, myPositionY, EnemyPositionX, EnemyPositionY)
end

function GetLordShape(Lord)
	return ARMY.GetShape(Lord)
end

function GetLordHp(Lord)
	return ARMY.GetHP(Lord)
end

function GetClosestAdvantageOwnTowerID( EnemyShape, AILUATable )
	local TowerArray = ARMY.GetInRangeID( AILUATable.myArmyID, "TOWER", 2000)
	
	local i = 1
	while TowerArray[i] ~= nil do
		if (GAMERULE.IsShapeStronger(TOWER.GetShape(TowerArray[i]), EnemyShape)) and (TOWER.GetOwnerTeam(TowerArray[i]) == ARMY.GetTeam(myArmyID)) then
			return TowerArray[i]
		end
		i = i + 1
	end
	return -1
end

function GetClosestAdvantageTowerID(EnemyShape)
	local TowerArray = ARMY.GetInRangeID(myArmyID, "TOWER", 5000)
	
	local i = 1
	while TowerArray[i] ~= nil do
		if GAMERULE.IsShapeStronger(TOWER.GetShape(TowerArray[i]), EnemyShape) then
			return TowerArray[i]
		end
		i = i + 1
	end
	return -1
end

--********************************************************************
--                              Helper                              
--********************************************************************

function AttackLord( AILUATable )
	local EnemyID = GetClosestVisibleEnemyArmyID( AILUATable )
	local EnemyDif = GetEnemyDif( EnemyID, AILUATable )
	
	if EnemyDif < ENEMY_NEAR_ENOUGH_IN_SPECIAL_ABILITY_RANGE and ARMY.IsCanUseSpell( AILUATable.myArmyID ) then
		ARMY.UseSpecialAbility( AILUATable.myArmyID )
	else
		AttackEnemy( EnemyID, AILUATable )
	end
end

function CaptureTower( Tower, AILUATable )
	if TOWER.GetOwnerTeam(Tower) == ARMY.GetTeam( AILUATable.myArmyID ) then
		ChangeShape( Tower, AILUATable )
	else
		Capture( Tower, AILUATable )
	end
end

--********************************************************************
--                              Behavior                              
--********************************************************************

function Capture( Tower, AILUATable )
	ARMY.SetBehavior( AILUATable.myArmyID, "Capturing" )
	ARMY.SetGoal( AILUATable.myArmyID, "TOWER", Tower )
end

function ChangeShape( Tower, AILUATable )
	ARMY.SetBehavior( AILUATable.myArmyID, "Changing" )
	ARMY.SetGoal( AILUATable.myArmyID, "TOWER", Tower )
end

function AttackCastle( Castle, AILUATable )
	ARMY.SetBehavior( AILUATable.myArmyID, "AttackCastle")
	ARMY.SetGoal( AILUATable.myArmyID, "CASTLE", Castle )
end

function AttackEnemy( Enemy, AILUATable )
	ARMY.SetBehavior( AILUATable.myArmyID, "AttackLord")
	ARMY.SetGoal( AILUATable.myArmyID, "ARMY", Enemy)
end

function Idle( AILUATable )
	local myPositionX, myPositionY = ARMY.GetPosition( AILUATable.myArmyID )
	ARMY.SetBehavior( AILUATable.myArmyID, "WalkTo" )
	ARMY.SetGoalPosition( AILUATable.myArmyID, myPositionX, myPositionY )
end

function WalkTo( PositionX, PositionY, AILUATable )
	ARMY.SetBehavior( AILUATable.myArmyID, "WalkTo" )
	ARMY.SetGoalPosition( AILUATable.myArmyID, PositionX, PositionY)
end