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

function Init ()
--WANDER.AddGoalPoint(myArmyID, 1000, 1000, 0)
--WANDER.AddGoalPoint(myArmyID, 1000, -1000, 0)
--WANDER.AddGoalPoint(myArmyID, -1000, -1000, 0)
--WANDER.AddGoalPoint(myArmyID, -1000, 1000, 0)
  WanderIndex = 0
  Enter_MoveNavigation()
end

function Enter_MoveNavigation ()
  local MoveGoalX, MoveGoalY, MoveGoalZ = WANDER.GetGoalPoint(myArmyID, WanderIndex)
  ARMY.SetBehavior(myArmyID, "WalkTo")
  ARMY.SetGoalPosition(myArmyID, MoveGoalX, MoveGoalY)
  STATE = "MoveNavigation"
end

function Enter_Idle ()
  Idle_Start_Time = TIME.GetGameNowTime()
  ARMY.SetBehavior(myArmyID, "Idle")
  STATE = "Idle"
end

function Enter_Pursuit(AttackArmyID)
  Pre_State = STATE
  Pursuit_Start_PositionX, Pursuit_Start_PositionY = ARMY.GetPosition(myArmyID)
  Pursuit_Start_Time = TIME.GetGameNowTime()
  Pursuit_Goal_Army = AttackArmyID

  ARMY.SetBehavior(myArmyID, "AttackLord")
  ARMY.SetGoal(myArmyID, "ARMY", Pursuit_Goal_Army)

  STATE = "Pursuit"
end

function Enter_Capture (iGoalTowerID)
  Capture_Goal_Tower = iGoalTowerID
  ARMY.SetBehavior(myArmyID, "Capturing")
  ARMY.SetGoal(myArmyID, "TOWER", Capture_Goal_Tower)
  STATE = "Capture"
end

function Exit_Pursuit()
  if Pre_State == "MoveNavigation" then
    Enter_MoveNavigation()
  elseif Pre_State == "Idle" then
    Enter_MoveNavigation()
  elseif Pre_State == "Capture" then
    Enter_Capture(Capture_Goal_Tower)
  end
end

function Update_MoveNavigation ()
  if ARMY.IsInGoalRange(myArmyID) then
    Enter_Idle()
    return 1
  end

  local TowerID = ARMY.GetInRangeID(myArmyID, "ENEMY_CAPTURED_TOWER", 1000)
  if table.getn(TowerID) > 0 then
    Enter_Capture(TowerID[1])
    return 1
  end

  local EnemyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", 1000)
  if table.getn(EnemyID) > 0 then
    Enter_Pursuit(EnemyID[1])
    return 1
  end  
end

function Update_Idle ()
  local ArmyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", 1000)
  if table.getn(ArmyID) > 0 then
    Enter_Pursuit(ArmyID[1])
    return 1
  end  

  local TimeDif = TIME.GetGameNowTime() - Idle_Start_Time
  if TimeDif > 2 then
    iSize = WANDER.GetGoalSize(myArmyID)
    if WanderIndex < ( iSize-1) then
      WanderIndex = WanderIndex +1
      Enter_MoveNavigation()
    end
    return 1
  end
end

function Update_Pursuit ()
  if IsThisArmyNotNeedAttack(Pursuit_Goal_Army) then
    Exit_Pursuit()
    return 1
  else
    local TimeDif = TIME.GetGameNowTime() - Pursuit_Start_Time
    if TimeDif > 2 then
      Exit_Pursuit()
      return 1
    else
      local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
      local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(Now_PositionX, Now_PositionY, Pursuit_Start_PositionX, Pursuit_Start_PositionY)
      if DistanceFromOrigin > 300 then
        Exit_Pursuit()
        return 1
      end
    end
  end
end

function Update_Capture ()
  local towerTeam = TOWER.GetOwnerTeam(Capture_Goal_Tower)
  if towerTeam == ARMY.GetTeam(myArmyID) then 
    Enter_MoveNavigation()
    return 1
  elseif ARMY.IsBeenAttacked(myArmyID) then
    local EnemyID = ARMY.GetAttackerID(myArmyID)
    Enter_Pursuit(EnemyID)
    return 1
  end  
end

function Update ()
  if STATE == "MoveNavigation" then
    Update_MoveNavigation()
  elseif STATE == "Idle" then
    Update_Idle()
  elseif STATE == "Pursuit" then
    Update_Pursuit()
  elseif STATE == "Capture" then
    Update_Capture()
  end
end
