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

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

function MultiPlayer_GetClosestPowerUpID( AILUATable )
	return ARMY.GetClosestID( AILUATable.myArmyID, "POWERUP" )
end

function MultiPlayer_GetClosestUpgradeItemID( AILUATable )
	return ARMY.GetClosestID( AILUATable.myArmyID, "UPGRADEITEM" )
end

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

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

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

function MultiPlayer_GetPowerUpDif( AILUATable, PowerUp )
	local myPositionX, myPositionY = ARMY.GetPosition( AILUATable.myArmyID )
	local PowerUpPositionX, PowerUpPositionY = POWERUP.GetPosition( PowerUp )
	return MAP.GetDistanceBetweenTwoPosition(myPositionX, myPositionY, PowerUpPositionX, PowerUpPositionY)
end

function MultiPlayer_GetUpgradeItemDif( AILUATable, UpgradeItem )
	local myPositionX, myPositionY = ARMY.GetPosition( AILUATable.myArmyID )
	local UpgradeItemPositionX, UpgradeItemPositionY = UPGRADEITEM.GetPosition(UpgradeItem)
	return MAP.GetDistanceBetweenTwoPosition(myPositionX, myPositionY, UpgradeItemPositionX, UpgradeItemPositionY)
end

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

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

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

function MultiPlayer_GetClosestAdvantageOwnTowerID( AILUATable, EnemyShape )
	local TowerArray = ARMY.GetInRangeID( AILUATable.myArmyID, "TOWER", 2000 )
	
	if( TowerArray == -1) then
		return -1
	end
	
	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 MultiPlayer_GetClosestAdvantageTowerID( AILUATable, EnemyShape )
	local TowerArray = ARMY.GetInRangeID( AILUATable.myArmyID, "TOWER", 5000 )
	
	if( TowerArray == -1) then
		return -1
	end
	
	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 MultiPlayer_AttackLord( AILUATable )
	local EnemyID = MultiPlayer_GetClosestVisibleEnemyArmyID( AILUATable )
	local EnemyDif = MultiPlayer_GetEnemyDif( AILUATable, EnemyID )
	
	if EnemyDif < AILUATable.ENEMY_NEAR_ENOUGH_IN_SPECIAL_ABILITY_RANGE and ARMY.IsCanUseSpell( AILUATable.myArmyID) then
		ARMY.UseSpecialAbility( AILUATable.myArmyID )
	else
		MultiPlayer_AttackEnemy( AILUATable, EnemyID )
	end
end

function MultiPlayer_CaptureTower( AILUATable, Tower )
	if TOWER.GetOwnerTeam(Tower) == ARMY.GetTeam( AILUATable.myArmyID ) then
		MultiPlayer_ChangeShape( AILUATable, Tower )
	else
		MultiPlayer_Capture( AILUATable, Tower )
	end
end

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

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

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

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

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

function MultiPlayer_TakePowerUp( AILUATable, PowerUpID )
	local PowerUpPositionX, PowerUpPositionY = POWERUP.GetPosition(PowerUpID)
	ARMY.SetBehavior( AILUATable.myArmyID, "GetPowerUP" )
	ARMY.SetGoalPosition( AILUATable.myArmyID, PowerUpPositionX, PowerUpPositionY )
end

function MultiPlayer_TakeUpgradeItem( AILUATable, UpgradeItemID )
	local UpgradeItemPositionX, UpgradeItemPositionY = UPGRADEITEM.GetPosition(UpgradeItemID)
	ARMY.SetBehavior( AILUATable.myArmyID, "GetUpgradeItem" )
	ARMY.SetGoalPosition( AILUATable.myArmyID, UpgradeItemPositionX, UpgradeItemPositionY )
end

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

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