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





function Patrol_And_Chase_Enter_Wander ( AILUATable )
  AILUATable.STATE = "Wander"
  ARMY.SetBehavior(AILUATable.myArmyID, "Wander")
end



function Patrol_And_Chase_Update_Wander ( AILUATable )      
  local needToAttackArmy = false
  local EnemyID = ARMY.GetInRangeID(AILUATable.myArmyID, 
"VISIBLE_CAN_ATTACK_ARMY", startChasingArmyRange)

  if table.getn(EnemyID) > 0 then
    needToAttackArmy = true
  elseif ARMY.IsBeenAttacked(AILUATable.myArmyID) then
    needToAttackArmy = true
    EnemyID = ARMY.GetAttackerID(AILUATable.myArmyID)
  end
  
  if needToAttackArmy then
    Patrol_And_Chase_Exit_Wander ( AILUATable )
    Patrol_And_Chase_Enter_AttackLord( AILUATable )
  end 
end



function Patrol_And_Chase_Exit_Wander ( AILUATable )
  Wander_Exit_PositionX, Wander_Exit_PositionY = ARMY.GetPosition(AILUATable.myArmyID)
end




function Patrol_And_Chase_Enter_AttackLord ( AILUATable, AttackLord_Goal_Army  )
  AILUATable.STATE = "AttackLord"
  
  AttackLord_Goal_Army = EnemyID
  hasStoppedApproachingEnemy = false
  
  ARMY.SetBehavior(AILUATable.myArmyID, "WalkTo")
  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  ARMY.SetGoalPosition(AILUATable.myArmyID, Enemy_PositionX, Enemy_PositionY)  
end


function Patrol_And_Chase_Update_AttackLord ( AILUATable )
  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(Enemy_PositionX, Enemy_PositionY, Wander_Exit_PositionX, Wander_Exit_PositionY)
      
  if (DistanceFromOrigin > stopChasingArmyRange) or (not (IsAttackable(AttackLord_Goal_Army))) 
  then
    Patrol_And_Chase_Exit_AttackLord ( AILUATable )
    Patrol_And_Chase_Enter_WalkBackToPatrol( AILUATable )
  else
    local Now_PositionX, Now_PositionY = ARMY.GetPosition(AILUATable.myArmyID)
    local DistanceFromEnemy = MAP.GetDistanceBetweenTwoPosition(Now_PositionX, Now_PositionY, Enemy_PositionX, Enemy_PositionY)
    if DistanceFromEnemy < stopApproachingRange then
      if not hasStoppedApproachingEnemy then
        --already very close, stop approaching
        ARMY.SetGoalPosition(AILUATable.myArmyID, Now_PositionX, Now_PositionY)
        hasStoppedApproachingEnemy = true
      end
    else
      --keep approaching
      ARMY.SetGoalPosition(myArmyID, Enemy_PositionX, Enemy_PositionY)
      hasStoppedApproachingEnemy = false
    end
  end
end



function Patrol_And_Chase_Exit_AttackLord ( AILUATable )
  if ARMY.IsBeenAttacked(AILUATable.myArmyID) then
    --do nothing, calling here is just to clear the flag
  end 
end




function Patrol_And_Chase_Enter_WalkBackToPatrol ( AILUATable )
  AILUATable.STATE = "WalkBackToPatrol"
  
  ARMY.SetBehavior(AILUATable.myArmyID, "WalkTo")
  ARMY.SetGoalPosition(AILUATable.myArmyID, Wander_Exit_PositionX, Wander_Exit_PositionY)
end


function Patrol_And_Chase_Update_WalkBackToPatrol ( AILUATable )

  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(Enemy_PositionX, Enemy_PositionY, Wander_Exit_PositionX, Wander_Exit_PositionY)
  local CurPosX, CurPosY = ARMY.GetPosition(AILUATable.myArmyID)
  local DistanceFromLastPosition = MAP.GetDistanceBetweenTwoPosition(CurPosX, CurPosY, Wander_Exit_PositionX, Wander_Exit_PositionY)
      
  if (DistanceFromOrigin < startChasingArmyRange) and (IsAttackable(AttackLord_Goal_Army)) then
    Patrol_And_Chase_Exit_Exit_WalkBackToPatrol ( AILUATable )
    Patrol_And_Chase_Exit_Enter_AttackLord(AILUATable, AttackLord_Goal_Army)
  elseif DistanceFromLastPosition < WANDER_GOAL_DIF then
    Patrol_And_Chase_Exit_WalkBackToPatrol ( AILUATable )
    Patrol_And_Chase_Enter_Wander( AILUATable )
  end  
end



function Patrol_And_Chase_Exit_WalkBackToPatrol ( AILUATable )
  if ARMY.IsBeenAttacked(AILUATable.myArmyID) then
    --do nothing, calling here is just to clear the flag
  end 
end



function Patrol_And_Chase_AI_Init ( AILUATable )
  startChasingArmyRange = 1100
  stopChasingArmyRange = 2100
  stopApproachingRange = 400
  WANDER_GOAL_DIF = 300
  Patrol_And_Chase_Enter_Wander( AILUATable )
end


function Patrol_And_Chase_AI_Update ( AILUATable )
  if AILUATable.STATE == "Wander" then
    Patrol_And_Chase_Update_Wander( AILUATable )
  elseif AILUATable.STATE == "AttackLord" then
    Patrol_And_Chase_Update_AttackLord( AILUATable )
  elseif AILUATable.STATE == "WalkBackToPatrol" then
    Patrol_And_Chase_Update_WalkBackToPatrol( AILUATable )
  end
end
