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


--******************************************************************
--                             GLOBALS                              
--******************************************************************
SIGHT_DISTANCE = 1200
ENEMY_NEAR_ENOUGH_IN_SPECIAL_ABILITY_RANGE = 500


--******************************************************************
--                              Normal                              
--******************************************************************
function Enter_Normal( AILUATable )
	AILUATable.State = "normal"
end

function Check_Castle( AILUATable )
	AILUATable.Check = "castle"
	
	local CastleID = GetClosestCastleID( AILUATable )
	local ACIDamage = ARMY.GetACIDamage( AILUATable.myArmyID )
	if ACIDamage >= 30 then
		AttackCastle( CastleID, AILUATable )
	else
		Check_Fight( AILUATable )
	end
end

function Check_Fight( AILUATable )
	AILUATable.Check = "fight"
	local EnemyID = GetClosestVisibleEnemyArmyID( AILUATable )
	
	if(EnemyID == -1) then
		Check_ACI( AILUATable )
		return 1
	end
	
	local EnemyDif = GetEnemyDif( EnemyID, AILUATable )
	local MyShape = GetLordShape( AILUATable.myArmyID )
	local MyHp = GetLordHp( AILUATable.myArmyID )
	local EnemyShape = GetLordShape( EnemyID )
	local EnemyHp = GetLordHp( EnemyID )
	
	if EnemyDif < SIGHT_DISTANCE then
		if GAMERULE.IsShapeStronger(MyShape, EnemyShape) then
			AttackLord( AILUATable )
		elseif GAMERULE.IsShapeSame(MyShape, EnemyShape) then
			if MyHp >= EnemyHp then
				AttackLord( AILUATable )
			else
				AILUATable.g_MyShape = MyShape
				AILUATable.g_EnemyShape = EnemyShape
				Enter_Escape( AILUATable )
			end
		else
			AILUATable.g_MyShape = MyShape
			AILUATable.g_EnemyShape = EnemyShape
			Enter_Escape( AILUATable )
		end
	else
		Check_ACI( AILUATable )
	end
end

function Check_ACI( AILUATable )
	AILUATable.Check = "aci"
	local ACITowerID = ARMY.GetClosestID( AILUATable.myArmyID, "OWN_ACI" )
	local ACIDif = GetTowerDif( ACITowerID, AILUATable )
	if (ACITowerID ~= -1) and (ACIDif < SIGHT_DISTANCE) then
		fACITowerPosX, fACITowerPosY = TOWER.GetPosition(ACITowerID)
		WalkTo( fACITowerPosX, fACITowerPosY, AILUATable )
	else
		Check_Tower( AILUATable )
	end
end

function Check_Tower( AILUATable )
	AILUATable.Check = "tower"
	local GoalTowerArray = ARMY.GetInRangeID( AILUATable.myArmyID, "CAN_CAPTURE_TOWER", 100000)
	
	if table.getn(GoalTowerArray) == 0 then
		Check_ACI_Again( AILUATable )
	end
	
	if RandomTowerIndex == 0 then
		RandomTowerIndex = math.random(3)
		GoalTower = GoalTowerArray[RandomTowerIndex]
	end
	
	if TOWER.GetOwnerTeam(GoalTower) == ARMY.GetTeam( AILUATable.myArmyID ) then
		RandomTowerIndex = 0
	else
		Capture(GoalTower, AILUATable )
	end
end

function Check_ACI_Again( AILUATable )
	AILUATable.Check = "aci_again"
	local ACITowerID = ARMY.GetClosestID( AILUATable.myArmyID, "OWN_ACI" )
	
	if ACITowerID ~= -1 then
		fACITowerPosX, fACITowerPosY = TOWER.GetPosition(ACITowerID)
		WalkTo(fACITowerPosX, fACITowerPosY, AILUATable )
	else
		Idle( AILUATable )
	end
end

function Update_Normal( AILUATable )
	Check_Castle( AILUATable )
end

--******************************************************************
--                              Escape                              
--******************************************************************
function Enter_Escape ( AILUATable )
	AILUATable.State = "escape"
end

function Update_Escape ( AILUATable )
	local MyShape = GetLordShape( AILUATable.myArmyID )
	
	if( AILUATable.g_MyShape ~= MyShape ) then
		Enter_Normal( AILUATable )
	end
	
	AILUATable.Check = "escape_ownadvtower"
	local AdvantageTower = GetClosestAdvantageOwnTowerID( AILUATable.g_EnemyShape, AILUATable )
	if(AdvantageTower ~= -1) then
		CaptureTower( AdvantageTower, AILUATable )
		return 1
	end
	
	Check = "escape_advtower"
	local AdvantageTower = GetClosestAdvantageTowerID( AILUATable.g_EnemyShape, AILUATable )
	if(AdvantageTower ~= -1) then
		CaptureTower( AdvantageTower, AILUATable )
		return 1
	end
end

--******************************************************************
--                              General                              
--******************************************************************
function AI_Init( AILUATable )
	AILUATable.RandomTowerIndex = 0
	AILUATable.State = "normal"
	AILUATable.Check = "init"
	AILUATable.g_EnemyShape = 0
	AILUATable.g_MyShape = 0
end

function AI_Update( AILUATable )
	
	DEBUG.ShowMessage( AILUATable.myArmyID, AILUATable.Check )
	
	if AILUATable.State == "normal" then
	  Update_Normal( AILUATable )
	elseif AILUATable.State == "escape" then
	  Update_Escape( AILUATable )
	end
end