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

function Init ()
  Enter_AttackCaslte()
end

function Enter_AttackCaslte ()
  local EnemyCastleID = ARMY.GetClosestID(myArmyID, "CAN_ATTACK_CASTLE")
  ARMY.SetBehavior(myArmyID, "AttackCastle")
  ARMY.SetGoal(myArmyID, "ARMY", EnemyCastleID)
  STATE = "AttackCastle"
end

function Enter_Pursuit(AttackArmyID)
  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 Update_AttackCastle ()
  if ARMY.IsInGoalRange(myArmyID) then
    if ARMY.IsBeenAttacked(myArmyID) then
      local EnemyID = ARMY.GetAttackerID(myArmyID)
      Enter_Pursuit(EnemyID)
      return 1
    end
  else
    local ArmyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", 1000)
    if table.getn(ArmyID) > 0 then
      Enter_Pursuit(ArmyID[1])
      return 1
    end  
  end
end

function Update_Pursuit ()
  if IsThisArmyNotNeedAttack(Pursuit_Goal_Army) then
    Enter_AttackCaslte()
    return 1
  else
    local TimeDif = TIME.GetGameNowTime() - Pursuit_Start_Time
    if TimeDif > 3 then
      Enter_AttackCaslte()
      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 > 500 then
        Enter_AttackCaslte()
        return 1
      end
    end
  end
end

function Update ()
  if STATE == "AttackCastle" then
    Update_AttackCastle()
  elseif STATE == "Pursuit" then
    Update_Pursuit()
  end
end
