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


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

function MultiPlayer_Check_Castle( AILUATable )
	AILUATable.Check = "castle"
	DEBUG.Print("Check can attack castle-ACI valid")
	
	local CastleID = MultiPlayer_GetClosestCastleID( AILUATable )
	local ACIDamage = ARMY.GetACIDamage( AILUATable.myArmyID )
	if ACIDamage >= 90 then
		MultiPlayer_AttackCastle( AILUATable, CastleID )
	else
		MultiPlayer_Check_Power( AILUATable )
	end
end

function MultiPlayer_Check_Power( AILUATable )
	AILUATable.Check = "powerup"
	DEBUG.Print("Check powerups")
	local PowerUpID = MultiPlayer_GetClosestPowerUpID( AILUATable )
	local PowerUpDif = MultiPlayer_GetPowerUpDif( AILUATable, PowerUpID )
	
	if PowerUpDif < 750 then
		DEBUG.Print("Powerup - Taking the powerup")
		MultiPlayer_TakePowerUp( AILUATable, PowerUpID )
	else
		MultiPlayer_Check_Fight( AILUATable )
	end
end

function MultiPlayer_Check_Fight( AILUATable )
	AILUATable.Check = "fight"
	DEBUG.Print("Check Fight")
	local EnemyID = MultiPlayer_GetClosestVisibleEnemyArmyID( AILUATable )
	
	if(EnemyID == -1) then
		DEBUG.Print("Check fight - no enemy.")
		MultiPlayer_Check_CastleLevelUp( AILUATable )
		return 1
	end
	
	local EnemyDif = MultiPlayer_GetEnemyDif( AILUATable, EnemyID )
	
	if( EnemyDif == 0 ) then
		DEBUG.Print("Check fight - no enemy - dist apart is 0.")
		MultiPlayer_Check_CastleLevelUp( AILUATable )
		return 1
	end
	
	local MyShape = MultiPlayer_GetLordShape( AILUATable.myArmyID )
	local MyHp = MultiPlayer_GetLordHp( AILUATable.myArmyID )
	local EnemyShape = MultiPlayer_GetLordShape(EnemyID)
	local EnemyHp = MultiPlayer_GetLordHp(EnemyID)
	
	if EnemyDif < AILUATable.SIGHT_DISTANCE then
		DEBUG.Print("Fight - Enemy Dif"..EnemyDif.."sight dist"..AILUATable.SIGHT_DISTANCE)
		if GAMERULE.IsShapeStronger(MyShape, EnemyShape) then
			DEBUG.Print("F1 - attacking enemy lord")
			MultiPlayer_AttackLord( AILUATable )
		elseif GAMERULE.IsShapeSame(MyShape, EnemyShape) then
			if MyHp >= EnemyHp then
				DEBUG.Print("F2 - attacking enemy lord")
				MultiPlayer_AttackLord( AILUATable )
			else
				DEBUG.Print("F3 - escaping for better shape - low HP")
				AILUATable.g_MyShape = MyShape
				AILUATable.g_EnemyShape = EnemyShape
				MultiPlayer_Enter_Escape( AILUATable )
			end
		else
			DEBUG.Print("F4 - escaping for better shape")
			AILUATable.g_MyShape = MyShape
			AILUATable.g_EnemyShape = EnemyShape
			MultiPlayer_Enter_Escape( AILUATable )
		end
	else
		DEBUG.Print("F5 - leaving fight for castle level up")
		MultiPlayer_Check_CastleLevelUp( AILUATable )
		return 1
	end
	DEBUG.Print("F6 -- leaving fight")
end

function MultiPlayer_Check_CastleLevelUp( AILUATable )
	AILUATable.Check = "castlelevelup"
	local OwnCastleID = ARMY.GetOwnCastleID( AILUATable.myArmyID )
	local CastleUpgradePoint = CASTLE.GetUpgradePoint(OwnCastleID)
	local UpgradePoint = ARMY.GetUpgradePoint( AILUATable.myArmyID )
	
	DEBUG.Print("Checking CastleLevel Up:"..OwnCastleID..CastleUpgradePoint..UpgradePoint)
	if (UpgradePoint >= 5) and (CastleUpgradePoint < AILUATable.UPGRADE_POINT_TO_LEVEL_3) then
		ARMY.SetBehavior( AILUATable.myArmyID, "MoveBackCastle")
	else
		MultiPlayer_Check_UpgradeItem( AILUATable )
	end
end

function MultiPlayer_Check_UpgradeItem( AILUATable )
	AILUATable.Check = "upgradeitem"
	local UpgradeItemID = MultiPlayer_GetClosestUpgradeItemID( AILUATable )
	local UpgradeItemDif = MultiPlayer_GetUpgradeItemDif( AILUATable, UpgradeItemID)
	local OwnCastleID = ARMY.GetOwnCastleID( AILUATable.myArmyID )
	local CastleUpgradePoint = CASTLE.GetUpgradePoint(OwnCastleID)
	
	if(UpgradeItemID ~= -1) and (UpgradeItemDif < AILUATable.SIGHT_DISTANCE) and (CastleUpgradePoint < AILUATable.UPGRADE_POINT_TO_LEVEL_3 ) then
		MultiPlayer_TakeUpgradeItem( AILUATable, UpgradeItemID )
	else
		MultiPlayer_Check_ACI( AILUATable )
	end
end

function MultiPlayer_Check_ACI( AILUATable )
	AILUATable.Check = "aci"
	local ACITowerID = ARMY.GetClosestID( AILUATable.myArmyID, "OWN_ACI" )
	local ACIDif = MultiPlayer_GetTowerDif( AILUATable, ACITowerID )
	if (ACITowerID ~= -1) and (ACIDif < AILUATable.SIGHT_DISTANCE) then
		DEBUG.Print("Stay at own tower and wait on ACI")
		local fACITowerPosX, fACITowerPosY = TOWER.GetPosition(ACITowerID)
		MultiPlayer_WalkTo( AILUATable, fACITowerPosX, fACITowerPosY )
	else
		DEBUG.Print("Can't find own ACI - find new tower")
		MultiPlayer_Check_Tower( AILUATable )
	end
end

function MultiPlayer_Check_Tower( AILUATable )
	AILUATable.Check = "tower"
	DEBUG.Print("Tower")
	local GoalTowerArray = ARMY.GetInRangeID( AILUATable.myArmyID, "CAN_CAPTURE_TOWER", 10000 )

	if (GoalTowerArray == -1) or (table.getn(GoalTowerArray) == 0)
	then
		DEBUG.Print("T2 - can't find towers")
		AILUATable.RandomTowerIndex = -1
		MultiPlayer_Check_ACI_Again( AILUATable )
		return
	end
	
	-- if we don't already have a target tower then find one
	if AILUATable.RandomTowerIndex == 0 then
		DEBUG.Print("T3 - gonna grab towers")
		
		local RandTower =  math.random(table.getn(GoalTowerArray))
		
		while (GoalTowerArray[RandTower] == nil) do
			RandTower =  math.random(table.getn(GoalTowerArray))
		end
		AILUATable.RandomTowerIndex = RandTower
		AILUATable.GoalTower = GoalTowerArray[RandTower]
		
		--DEBUG.Print("T3a: Goal Tower Length"..table.getn(GoalTowerArray).."Random"..RandTower)
		DEBUG.Print("T3c: Goal Tower is"..AILUATable.GoalTower)
	end
	
	DEBUG.Print("T4")
	-- attempt to capture target tower if it is valid
	if TOWER.GetOwnerTeam( AILUATable.GoalTower ) == ARMY.GetTeam( AILUATable.myArmyID ) and
		TOWER.CanSpawnACI( AILUATable.GoalTower ) == 0
	then
		DEBUG.Print("T4a - find a new tower - this one doesn't have a soldier")
		AILUATable.RandomTowerIndex = 0
		return
	else
		DEBUG.Print("T4b - capture tower. This one SHOULD have a soldier at it!")
		MultiPlayer_Capture( AILUATable, AILUATable.GoalTower )
		return
	end
end

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

function MultiPlayer_Update_Normal( AILUATable )
	MultiPlayer_Check_Castle( AILUATable )
end

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

function MultiPlayer_Update_Escape( AILUATable )
	local MyShape = MultiPlayer_GetLordShape( AILUATable.myArmyID )
	
	if( AILUATable.g_MyShape ~= MyShape ) then
		MultiPlayer_Enter_Normal( AILUATable )
	end
	
	AILUATable.Check = "escape_powerup"
	local PowerUpID = MultiPlayer_GetClosestPowerUpID( AILUATable )
	local PowerUpDif = MultiPlayer_GetPowerUpDif( AILUATable, PowerUpID )
	if PowerUpDif < AILUATable.SIGHT_DISTANCE then
		MultiPlayer_TakePowerUp( AILUATable, PowerUpID )
		return 1
	end
	
	AILUATable.Check = "escape_ownadvtower"
	local AdvantageTower = MultiPlayer_GetClosestAdvantageOwnTowerID( AILUATable, AILUATable.g_EnemyShape )
	if(AdvantageTower ~= -1) then
		MultiPlayer_CaptureTower( AILUATable, AdvantageTower )
		return 1
	end
	
	AILUATable.Check = "escape_advtower"
	local AdvantageTower = MultiPlayer_GetClosestAdvantageTowerID( AILUATable, AILUATable.g_EnemyShape )
	if(AdvantageTower ~= -1) then
		MultiPlayer_CaptureTower( AILUATable, AdvantageTower )
		return 1
	end
end

--******************************************************************
--                              General                              
--******************************************************************


function MultiPlayer_AI_Init( AILUATable )
	AILUATable.SIGHT_DISTANCE = 2000
	AILUATable.ENEMY_NEAR_ENOUGH_IN_SPECIAL_ABILITY_RANGE = 500
	AILUATable.UPGRADE_POINT_TO_LEVEL_3 = 30
	AILUATable.RandomTowerIndex = 0
	
	AILUATable.State = "init"
	AILUATable.Check = "init"
	AILUATable.g_EnemyShape = 0
	AILUATable.g_MyShape = 0
	AILUATable.GoalTower = -1
	
	MultiPlayer_Enter_Normal( AILUATable )
end



function MultiPlayer_AI_Update( AILUATable )
	--DEBUG.ShowMessage( AILUATable.myArmyID, AILUATable.State )
	DEBUG.ShowMessage( AILUATable.myArmyID, AILUATable.Check )
	
	if AILUATable.State == "normal" then
		MultiPlayer_Update_Normal( AILUATable )
	elseif AILUATable.State == "escape" then
		MultiPlayer_Update_Escape( AILUATable )
	end
end

--TODO deal with no enemy/tower in the map...idle()



