public class Color {
	private ActiveXComponent activeDm=null;
	
	public Color(ActiveXComponent active) {
		this.activeDm=active;
	}
	
	/**
	 * 获取指定点的颜色
	 * @param x
	 * @param y
	 * @return 字符串形式16禁制FFFFFF
	 */
	public String getColor(int x,int y){
		String color="FFFFFF";
		try {
			color=activeDm.invoke("GetColor",x,y).getString();
		} catch (Exception e) {
			new Func.File().log("获取指定点颜色getColor异常捕获:"+e.getMessage());
		}
		return color;
	}
	
	/**
	 * 判断某点颜色是否跟指定的相同或相似
	 * @param color:要判断的颜色（FFFFFF）
     * @return 若颜色匹配，此点是这颜色，则返回true
	 */
	public boolean findColor(int x,int y,String color){
		boolean xnx3_result=false;
		try {
			Variant[] var=new Variant[4];
			var[0]=new Variant(x);
			var[1]=new Variant(y);
			var[2]=new Variant(color+"-"+Command.dianColor);
			var[3]=new Variant(Command.dianSim);
			
			int cmpColor=activeDm.invoke("CmpColor",var).getInt();
			var=null;

			if(cmpColor==0){
				xnx3_result=true;
			}
			cmpColor=0;
		} catch (Exception e) {
			new Func.File().log("获取颜色findColor异常捕获:"+e.getMessage());
		}
		return xnx3_result;
	}
	
	/**
	 * 范围区域内查询某颜色是否存在
	 * <li>color:查找的颜色，如"ffffff-333333"多个用|分割
	 * <li>sim:查找的精确度
	 * <li>dir:查询的顺序，1为从左到右从上到下，详细参考DM-FindColorE函数
	 * 
	 * @return  
	 * 若是成功返回坐在坐标int[0]：X坐标，int[1]：Y坐标
	 * 若是失败，则都是-1
	 */
	public int[] findColor(int xStart,int yStart,int xEnd,int yEnd,String color,double sim,int dir){
		int[] xnx3_result=new int[2];
		try {
			Variant[] var=new Variant[7];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(color);
			var[5]=new Variant(sim);
			var[6]=new Variant(dir);
			
			String findColor=activeDm.invoke("FindColorE",var).getString();
			String[] findColorArray=findColor.split("\\|");
			xnx3_result[0]=new Func.Lang().Integer_(findColorArray[0], -1);
			xnx3_result[1]=new Func.Lang().Integer_(findColorArray[1], -1);
			var=null;
			findColorArray=null;
			findColor=null;
		} catch (Exception e) {
			new Func.File().log("获取颜色findColor异常捕获:"+e.getMessage());
		}
		return xnx3_result;
	}
	
	/**
	 * 判断2个16进制数据的差
	 */
	public boolean compareColor(String color1,String color2,String param){
		boolean xnx3_result=false;
		if(color1==null||color1.length()!=6||color2==null||color2.length()!=6||param==null||param.length()!=6){
			//false
		}else{
			try {
				Lang lang=new Func.Lang();
				int color1_12=lang.Integer_(color1.substring(0, 2), 0, 16);
				int color1_34=lang.Integer_(color1.substring(2, 4), 0, 16);
				int color1_56=lang.Integer_(color1.substring(4, 6), 0, 16);

				int color2_12=lang.Integer_(color2.substring(0, 2), 9, 16);
				int color2_34=lang.Integer_(color2.substring(2, 4), 9, 16);
				int color2_56=lang.Integer_(color2.substring(4, 6), 9, 16);
				
				int param_12=lang.Integer_(param.substring(0, 2), 5, 16);
				int param_34=lang.Integer_(param.substring(2, 4), 5, 16);
				int param_56=lang.Integer_(param.substring(4, 6), 5, 16);
				
				if(Math.abs(color1_12-color2_12)<param_12&&Math.abs(color1_34-color2_34)<param_34&&Math.abs(color1_56-color2_56)<param_56){
					xnx3_result=true;
				}else{
					xnx3_result=false;
				}
				
				lang=null;
			} catch (Exception e) {
				e.printStackTrace();
				new Func.File().log("16进制判断异常捕获:"+e.getMessage());
			}
		}
		
		return xnx3_result;
	}
}
 public class File {
	private ActiveXComponent activeDm=null;
	public File(ActiveXComponent active) {
		this.activeDm=active;
	}
	public File() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * 播放声音
	 * 需定义播放文件：
	 * @param playFile:声音文件所在路径
	 * @param playTime:声音播放时间
	 */
	public void play(String playFile,int playTime){
		if(Command.playMusic){
			Command.playFile=playFile;
			Command.playTime=playTime;
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						int play=activeDm.invoke("Play",Command.playFile).getInt();
						new Func.Sleep().sleep(Command.playTime);
						if(play==0){
//							System.out.println("声音["+Command.playFile+"]播放失败！");
						}else{
							activeDm.invoke("Stop",play);
						}
						log("声音播放-"+(play==0? "失败":"成功"));
					} catch (Exception e) {
						log("声音播放异常捕获"+e.getMessage());
					}
				}
			}).start();
		}
	}
	
	/**
	 * 日志记录
	 */
	public void log(String log){
		try {
			if(Command.realtimeRemind){
				Command.JForm.logTextArea.setText(Command.JForm.logTextArea.getText().toString()+"\n"+log+"\t"+include.Module.Now_date.xnx3_time());
			}
			Command.logBuffer.append(Command.lineNotePad+log+"\t"+include.Module.Now_date.xnx3_time()+"  ");		//2个空格，用于正则匹配用。从txt读取时将2个空各替换为换行符
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 截图保存日志记录,同时添加时间
	 * @param message
	 */
	public void screenImage(String message){
		try {
			int enableDisplayDebug=activeDm.invoke("EnableDisplayDebug",1).getInt();
			if(enableDisplayDebug==0){
				log("EnableDisplayDebug-截图开启失败");
			}
			
			Variant[] var=new Variant[5];
			var[0]=new Variant(0);
			var[1]=new Variant(0);
			var[2]=new Variant(1024);
			var[3]=new Variant(768);
			var[4]=new Variant(Command.thisFilePath+"\\"+Command.screenNoteFile+"\\"+message+"-"+include.Module.Now_date.xnx3_time().replaceAll(":", "：")+".bmp");
			activeDm.invoke("CapTure",var);
			int capTurePre=activeDm.invoke("CapturePre",var[4]).getInt();
			var=null;
//			log("截图-->"+(capTurePre==0? "失败":"成功"));
			
			activeDm.invoke("EnableDisplayDebug",0);
		} catch (Exception e) {
			log("截图保存日志Func.screenImage()异常捕获:"+e.getMessage());
		}
	}
	
	/**
	 * 保存出bmp格式图片
	 * @param message:截图
	 * @param filePath
	 */
	public void screenImage(String message,int xStart,int yStart,int xEnd,int yEnd){
		try {
			int enableDisplayDebug=activeDm.invoke("EnableDisplayDebug",1).getInt();
			if(enableDisplayDebug==0){
				log("EnableDisplayDebug-截图开启失败");
			}
			
			Variant[] var=new Variant[5];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(Command.thisFilePath+"\\"+Command.screenNoteFile+"\\"+message+".bmp");
			activeDm.invoke("CapTure",var);
			int capTurePre=activeDm.invoke("CapturePre",var[4]).getInt();
			var=null;
//			log(message+"截图-->"+(capTurePre==0? "失败":"成功"));
			
			activeDm.invoke("EnableDisplayDebug",0);
		} catch (Exception e) {
			log("截图保存日志Func.screenImage()异常捕获:"+e.getMessage());
		}
	}
	
	/**
	 * 删除单个文件，java操作
	 * @param fileName 文件名，包含路径。如E:\\a\\b.txt
	 * @return boolean true：删除成功
	 */
	public boolean deleteFile(String fileName){
		boolean xnx3_result=false;
		
		java.io.File f=new java.io.File(fileName);
		if(f.isFile()&&f.exists()){
			f.delete();
			xnx3_result=true;
		}
		
		return xnx3_result;
	}
	
}
 public class FindPic {
	private ActiveXComponent activeDm=null;
	public FindPic(ActiveXComponent active) {
		this.activeDm=active;
	}
	/**
	 * 查找图片
	 * @param pic:图像集，多个用|分割，bmp格式,且边框为同一种颜色
	 * @return 找到图返回true,未找到返回false
	 */
	public boolean findPicE(int xStart,int yStart,int xEnd,int yEnd,String pic){
		boolean xnx3_result=false;
		try {
			Variant[] var=new Variant[8];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(pic);
			var[5]=new Variant(Command.picColor);
			var[6]=new Variant(Command.picSim);
			var[7]=new Variant(0);
			String findPicE=activeDm.invoke("FindPicE",var).getString();
			var=null;
			String[] getPic=findPicE.split("\\|");
			if(Integer.parseInt(getPic[0])>-1){
				xnx3_result=true;
			}
			findPicE=null;
			getPic=null;
		} catch (Exception e) {
			new Func.File().log("查找图片Func.findPicE()异常捕获:"+e.getMessage());
		}
		return xnx3_result;
	}
	
	/**
	 * 查找图片，返回找到的第一个图片的坐标
	 * <li>String pic:图像集，多个用|分割，24位色bmp格式,且边框为同一种颜色
	 * @return 返回找到的第一个图片位置，返回int[]   <li>int[0]:是否找到，没找到返回-1  <li>int[1]:找到的图像的x坐标  <li>int[2]:找到的图像的y坐标
	 */
	public int[] findPicEPosition(int xStart,int yStart,int xEnd,int yEnd,String pic){
		int[] xnx3_result=new int[3];
		//初始化，避免走catch
		xnx3_result[0]=-1;
		xnx3_result[1]=-1;
		xnx3_result[2]=-1;
		try {
			Variant[] var=new Variant[8];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(pic);
			var[5]=new Variant(Command.picColor);
			var[6]=new Variant(Command.picSim);
			var[7]=new Variant(0);
			String findPicE=activeDm.invoke("FindPicE",var).getString();
			var=null;
			String[] getPic=findPicE.split("\\|");
			xnx3_result[0]=Integer.parseInt(getPic[0]);
			xnx3_result[1]=Integer.parseInt(getPic[1]);
			xnx3_result[2]=Integer.parseInt(getPic[2]);
			findPicE=null;
			getPic=null;
		} catch (Exception e) {
			new Func.File().log("查找图片Func.findPicEPosition()异常捕获:"+e.getMessage());
		}

		return xnx3_result;
	}
	
	/**
	 * 查找图片，返回找到的第一个图片的坐标
	 * <li>String pic:图像集，多个用|分割，24位色bmp格式,且边框为同一种颜色
	 * <li>String picColor:偏离颜色范围,303030
	 * <li>double picSim:近似度，一般不变的图为1.0
	 * @return 返回找到的第一个图片位置，返回int[]   <li>int[0]:是否找到，没找到返回-1  <li>int[1]:找到的图像的x坐标  <li>int[2]:找到的图像的y坐标
	 */
	public int[] findPicEPosition(int xStart,int yStart,int xEnd,int yEnd,String pic,String picColor,double picSim){
		int[] xnx3_result=new int[3];
		//初始化，避免走catch
		xnx3_result[0]=-1;
		xnx3_result[1]=-1;
		xnx3_result[2]=-1;
		try {
			Variant[] var=new Variant[8];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(pic);
			var[5]=new Variant(picColor);
			var[6]=new Variant(picSim);
			var[7]=new Variant(0);
			String findPicE=activeDm.invoke("FindPicE",var).getString();
			var=null;
			String[] getPic=findPicE.split("\\|");
			xnx3_result[0]=Integer.parseInt(getPic[0]);
			xnx3_result[1]=Integer.parseInt(getPic[1]);
			xnx3_result[2]=Integer.parseInt(getPic[2]);
			findPicE=null;
			getPic=null;
		} catch (Exception e) {
			new Func.File().log("查找图片Func.findPicEPosition()异常捕获:"+e.getMessage());
		}

		return xnx3_result;
	}
	
}
 public class FindStr {
	private ActiveXComponent activeDm=null;
	public FindStr(ActiveXComponent active) {
		this.activeDm=active;
	}
	/**
	 * 查找文字，包含文字是否出现、出现的文字的范围
	 * @return 数据int[3]   <li>int[0]:是否查找到，若是没有则返回-1  <li>int[1]查找到的文字的X的值，没有返回-1   <li>int[2]查找到的文字的Y值，没有返回-1
	 */
	public int[] findStrE(int xStart, int yStart, int xEnd,int yEnd, String findString ,String colorFormat,double sim,int useDict ){
		int[] xnx3_result=new int[3];
		//初始化赋值，避免走catch
		xnx3_result[0]=-1;
		xnx3_result[1]=-1;
		xnx3_result[2]=-1;
		try{
			activeDm.invoke("UseDict",useDict);
			
			Variant[] var=new Variant[7];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(findString);
			var[5]=new Variant(colorFormat);
			var[6]=new Variant(sim);
			String findStrE=activeDm.invoke("FindStrE",var).getString();
			var=null;
			String[] findStrEArray=findStrE.split("\\|");
			xnx3_result[0]=Integer.parseInt(findStrEArray[0]);
			xnx3_result[1]=Integer.parseInt(findStrEArray[1]);
			xnx3_result[2]=Integer.parseInt(findStrEArray[2]);
			findStrE=null;
			findStrEArray=null;
		}catch (Exception e) {
			e.printStackTrace();
			new Func.File().log("查找文字异常捕获:"+e.getMessage());
		}
		
		return xnx3_result;
	}
	
	/**
	 * 读取文字
	 * <li>xStart,xEnd,yStart,yEnd   读取文字所在的区域
	 * <li>colorFormat:文字颜色及范围，如ffd800-888888，多个用|分割
	 * <li>sim:相似度，最大为1，一般0.9便可
	 * @return:若是没找到，返回""
	 * 
	 */
	public String readStr(int xStart,int yStart,int xEnd,int yEnd,String colorFormat,double sim,int useDict){
		String xnx3_result="";
		try {
			activeDm.invoke("UseDict",useDict);
			Variant[] var=new Variant[6];
			var[0]=new Variant(xStart);
			var[1]=new Variant(yStart);
			var[2]=new Variant(xEnd);
			var[3]=new Variant(yEnd);
			var[4]=new Variant(colorFormat);
			var[5]=new Variant(sim);
			xnx3_result=activeDm.invoke("Ocr",var).getString();
		} catch (Exception e) {
			e.printStackTrace();
			new Func.File().log("读取文字异常捕获:"+e.getMessage());
		}
		
		return xnx3_result;
	}
}
 public class Lang {
	
	/**
	 * 字符型转换为整数型
	 * <li>value：待转换的字符串
	 * <li>errorNum:try异常后的返回值
	 * @return
	 */
	public int Integer_(String value,int errorNum){
		int xnx3_result=0;
		
		//首先判断字符串不能为空
		if(value==null||value.equalsIgnoreCase("null")){
			xnx3_result=errorNum;
		}else{
			try {
				xnx3_result=Integer.parseInt(value);
			} catch (Exception e) {
				xnx3_result=errorNum;
			}
		}
		
		return xnx3_result;
	}
	
	/**
	 * 
	 * @param value  要转换的
	 * @param errorNum  转换出错异常时返回的值
	 * @param radix  进制。16进制转换则为16
	 * @return  返回int-10禁制
	 */
	public int Integer_(String value,int errorNum,int radix){
		int xnx3_result=0;
		
		//首先判断字符串不能为空
		if(value==null||value.equalsIgnoreCase("null")){
			xnx3_result=errorNum;
		}else{
			try {
				xnx3_result=Integer.parseInt(value, radix);
			} catch (Exception e) {
				xnx3_result=errorNum;
			}
		}
		
		return xnx3_result;
	}
}
 public class Mouse {
	private ActiveXComponent activeDm=null;
	private ActiveXComponent activeDBMouseKey=null;
	public Mouse(ActiveXComponent active) {
		this.activeDm=active;
	}
	public Mouse(ActiveXComponent activeDBMouseKey,ActiveXComponent activeDm){
		this.activeDBMouseKey=activeDBMouseKey;
		this.activeDm=activeDm;
	}
	
	
	/**
	 * 鼠标移动至某个坐标点点击鼠标左键
	 * @param click：boolean若是点左键为true，右键为false
	 */
	public void mouseClick(int x,int y,boolean click){
		try {
			activeDm.invoke("MoveTo",x,y);
			new Func.Sleep().sleep(300);	//避免卡屏
			if(click){
				activeDm.invoke("LeftClick");
			}else{
				activeDm.invoke("RightClick");
			}
		} catch (Exception e) {
			new Func.File().log("日志记录异常捕获:"+e.getMessage());
		}
	}
	
	/**
	 * 鼠标点击
	 * <li>左键点击传入true
	 * <li>右键点击传入false
	 */
	public void mouseClick(boolean leftClick){
		try {
			if(leftClick){
				activeDm.invoke("LeftClick");
			}else{
				activeDm.invoke("rightClick");
			}
		} catch (Exception e) {
			new Func.File().log("鼠标点击mouseClick异常捕获:"+e.getMessage());
		}
	}
	
	/**
	 * 鼠标移动到指定点
	 */
	public void mouseMoveTo(int x,int y){
		try {
			activeDm.invoke("MoveTo",x,y);
		} catch (Exception e) {
			new Func.File().log("鼠标移动捕获异常:"+e.getMessage());
		}
	}
	
	/**
	 * 按住鼠标左键
	 */
	public void leftDown(){
		try {
			activeDm.invoke("LeftDown");
		} catch (Exception e) {
			new Func.File().log("鼠标左键按住捕获异常:"+e.getMessage());
		}
	}
	
	/**
	 * 弹起鼠标左键
	 */
	public void leftUp(){
		try {
			activeDm.invoke("LeftUp");
		} catch (Exception e) {
			new Func.File().log("鼠标左键弹起捕获异常:"+e.getMessage());
		}
	}
	
	/**
	 * 获取鼠标当前的坐标（大屏幕坐标）
	 * 使用DBSoft
	 * 
	 * @return int[2]x、y坐标
	 * 			若是获取失败，返回坐标{-1,-1}
	 */
	public int[] getMousePosition(){
		int[] xnx3_result={-1,-1};
		
		int[] getArray=null;
		try {
			getArray=this.activeDBMouseKey.invoke("GetCursorXY").toSafeArray().toIntArray();
			xnx3_result[0]=getArray[0];
			xnx3_result[1]=getArray[1];
		} catch (Exception e) {
			xnx3_result[0]=-1;
			xnx3_result[0]=-1;
			e.printStackTrace();
			new Func.File().log("获取鼠标当前坐标异常捕获:"+e.getMessage());
		}finally{
			getArray=null;
		}
		
		return xnx3_result;
	}
}
 public class Position {
	private ActiveXComponent activeDm=null;
	public Position(ActiveXComponent active) {
		this.activeDm=active;
	}
	
	/**
	 * 获取小地图x坐标
	 * @return 失败则返回0
	 */
	public int getMapPositionX(){
		int result=0;
		try{
			activeDm.invoke("UseDict",1).getInt();
			
			Variant[] var=new Variant[6];
			var[0]=new Variant(Command.mapXPositionXStart);
			var[1]=new Variant(Command.mapXPositionYStart);
			var[2]=new Variant(Command.mapXPositionXEnd);
			var[3]=new Variant(Command.mapXPositionYEnd);
			var[4]=new Variant(Command.mapPositionColor);
			var[5]=new Variant(Command.mapPositionSim);
			String returnNum=activeDm.invoke("Ocr",var).getString();
			result=Integer.parseInt(returnNum);
			var=null;
			returnNum=null;
		}catch (Exception e) {
			new Func.File().log("获取小地图X坐标异常捕获:"+e.getMessage());
		}
		return result;
	}
	
	/**
	 * 获取小地图y坐标
	 * @return 失败返回0
	 */
	public int getMapPositionY(){
		int result=0;
		try{
			activeDm.invoke("UseDict",1).getInt();
			Variant[] var=new Variant[6];
			var[0]=new Variant(Command.mapYPositionXStart);
			var[1]=new Variant(Command.mapYPositionYStart);
			var[2]=new Variant(Command.mapYPositionXEnd);
			var[3]=new Variant(Command.mapYPositionYEnd);
			var[4]=new Variant(Command.mapPositionColor);
			var[5]=new Variant(Command.mapPositionSim);
			String returnNum=activeDm.invoke("Ocr",var).getString();
			result=Integer.parseInt(returnNum);
			var=null;
			returnNum=null;
		}catch (Exception e) {
			new Func.File().log("获取小地图Y坐标异常捕获:"+e.getMessage());
		}
		
		return result;
	}
	
	
	/**
	 * 调整小地图方向，面向北
	 */
	public void mapArrowsInit(){
		int numMax=300;	//设置最大次数为300次
		
		Mouse mouse=new Mouse(activeDm);
		Press press=new Press(activeDm);
		Sleep sleep=new Sleep();
		File file=new File(activeDm);
		Invoke invoke=new Invoke(activeDm);
		FindPic findPic=new FindPic(activeDm);
		Color color=new Func.Color(this.activeDm);
		
		//首先走动一下，避免出现暂离
		press.keyPress(87);		//W
		
		sleep.sleep(500);
		press.keyPress(65);		//A
		sleep.sleep(3000);
		
		//M 开地图，取消之前的寻路
		press.keyPress(77);	
		sleep.sleep(500);
		mouse.mouseClick(748,569, true);
		sleep.sleep(200);
		press.keyPress(77);
		
		file.log("等待8秒，等待小地图闪烁取消");
		sleep.sleep(8000);
		
		file.log("进行方向偏移调整");
		
//		int dian1X=920;
//		int dianY=106;
//		int dian2X=940;
//		//提取小地图扇形弧度范围内上方的2个点的坐标颜色
//		String dian1Color1=null;	//其中一中颜色
//		String dian2Color1=null;
//		String dian1Color2=null;	
//		String dian2Color2=null;
//		int dian1Color1Num=0;		//出现的次数
//		int dian1Color2Num=0;
//		int dian2Color1Num=0;
//		int dian2Color2Num=0;
//		
//		String param="222222";		//误差模糊匹配
//		
//		//错误匹配记录,超过5此进行邮件提醒。理论上应该不存在
//		int dian1Error=0;
//		int dian2Error=0;
//		
//		//找图，直到找到图或者到达找图的次数为止
//		int i=0;
//		while(i++<numMax){
//			String getColor1=color.getColor(dian1X,dianY);	//点1
//			String getColor2=color.getColor(dian2X,dianY);	//点2
//			System.out.println("捕获两点颜色:点1："+getColor1+"|点2："+getColor2);
//			//首先对这两个点赋值
//			if(dian1Color1==null||dian1Color2==null){
//				if(dian1Color1==null){
//					dian1Color1=getColor1;
//				}else if (color.compareColor(dian1Color1, getColor1, param)) {
//					dian1Color1Num++;
//				}else if (dian1Color2==null) {
//					dian1Color2=getColor1;
//				}
//			}
//			if(dian2Color1==null||dian2Color2==null){
//				if(dian2Color1==null){
//					dian2Color1=getColor2;
//				}else if (color.compareColor(dian2Color1, getColor2, param)) {
//					dian2Color1Num++;
//				}else if (dian2Color2==null) {
//					dian2Color2=getColor2;
//				}
//			}
//			//两个点赋值结束
//			
//			if(color.compareColor(dian1Color1, getColor1, param)){
//				dian1Color1Num++;
//			}else if (color.compareColor(dian1Color2, getColor1, param)) {
//				dian1Color2Num++;
//			}else{
//				dian1Error++;
//			}
//			
//			if(color.compareColor(dian2Color1, getColor2, param)){
//				dian2Color1Num++;
//			}else if (color.compareColor(dian2Color2, getColor2, param)) {
//				dian2Color2Num++;
//			}else{
//				dian2Error++;
//			}
//			
//			
//			//进行默认固定方向偏移
//			mouse.mouseMoveTo(501, 150);
//			invoke.invoke("RightDown");
//			sleep.sleep(50);
//			mouse.mouseMoveTo(510, 180);
//			sleep.sleep(300);
//			invoke.invoke("RightUp");
//			
//			sleep.sleep(300);
//		}
//		//这种错误理论上应该不存在
//		if(dian1Error>5||dian2Error>5){
//			file.log("方向偏移异常");
////			new SendMail().send("水底开鼓方向偏移出现异常", "水底被杀后，方向水底开鼓方向偏移出现异常，建议请进入游戏查看");
//		}
//		
//		//循环完毕，进行最终覆盖点判断,提取覆盖点应该的颜色
//		String d1=null;
//		String d2=null;
//		if(dian1Color1Num>dian1Color2Num){
//			d1=dian1Color2;
//		}else{
//			d1=dian1Color1;
//		}
//		if(dian2Color1Num>dian2Color2Num){
//			d2=dian2Color2;
//		}else{
//			d2=dian2Color1;
//		}
//		//至此颜色匹配最终完毕
//		
//		//为了防止人物因为时间过长出现暂离现象，防御一下，解除暂离状态
//		press.keyPress(32);
//		sleep.sleep(1000);
//		press.keyPress(32);
//		System.out.println("dian1-1"+dian1Color1+">"+dian1Color1Num+"|dian1-2"+dian1Color2+">"+dian1Color2Num);
//		System.out.println("dian1-1"+dian2Color1+">"+dian2Color1Num+"|dian1-2"+dian2Color2+">"+dian2Color2Num);
//		System.out.println("error:"+dian1Error+"|"+dian2Error);
//		
//		boolean setIng=true;		//是否在调整中，若是调整成功，为false
//		int j=0;
//		while(j++<numMax){
//			boolean dian1Yes=color.compareColor(color.getColor(dian1X, dianY), d1, param);
//			boolean dian2Yes=color.compareColor(color.getColor(dian2X, dianY), d2, param);
//			System.out.println(dian1Yes+"|"+dian2Yes);
//			if(dian1Yes&&dian2Yes&&setIng){
//				setIng=false;
//				j=numMax;
//				file.log("识别成功，记录");
//			}else{		//两个都不匹配
//				mouse.mouseMoveTo(501, 150);
//				invoke.invoke("RightDown");
//				sleep.sleep(50);
//				mouse.mouseMoveTo(510, 180);
//				sleep.sleep(300);
//				invoke.invoke("RightUp");
//			}
//			
//			sleep.sleep(500);
//		}
//		
		int i=0;
		while(i++<numMax){
			int findArrows[]=findPic.findPicEPosition(905,103,967,147, "mapArrows.bmp", "050505", 0.9);
			if(findArrows[0]!=-1){	//找到了
				i=332;	//退出
			}else{
				mouse.mouseMoveTo(501, 150);
				invoke.invoke("RightDown");
				sleep.sleep(50);
				mouse.mouseMoveTo(510, 180);
				sleep.sleep(50);
				invoke.invoke("RightUp");
				sleep.sleep(500);
			}
		}
		
		//W向前走一步，确定方向，避免出现方向回滚
		press.keyPressTime(87, 1000);
		
		if(i==333){
			file.log("方向确立完毕!");
		}else{
			file.log("方向偏移失败！");
			new SendMail().send("水底开鼓方向偏移失败", "水底被杀后，方向自动识别失败，可能是被人杀的次数过多受伤过重！请进入游戏查看");
		}
		
		
		mouse=null;
		press=null;
		sleep=null;
		file=null;
		invoke=null;
		findPic=null;
	}
}
 public class Press {
	private ActiveXComponent activeDm=null;
	public Press(ActiveXComponent active) {
		this.activeDm=active;
	}
	public Press(){
		
	}
	/**
	 * 按下指定的键一定的时间后放开
	 * @param keyCode
	 * @param sleep
	 */
	public void keyPressTime(int keyCode,int sleep){
		try {
			activeDm.invoke("KeyDown",keyCode);
			
			try {
				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			activeDm.invoke("KeyUp",keyCode);
		} catch (Exception e) {
			new Func.File().log("按下指定键一段时间异常捕获:"+e.getMessage());
		}
	}
	
	/**
	 * 按下某键
	 * <li>keyCode：传入按键码
	 */
	public void keyPress(int keyCode){
		try {
			activeDm.invoke("KeyPress",keyCode);
		} catch (Exception e) {
			new Func.File().log("按键异常捕获"+e.getMessage());
		}
	}
	
	
	/**
	 * 按住某键(按下某个键位并不弹起)搭配keyUp使用
	 * <li>keyCode:传入按键码
	 */
	public void keyDown(int keyCode){
		try {
			activeDm.invoke("KeyDown",keyCode);
		} catch (Exception e) {
			new Func.File().log("按键按住异常捕获"+e.getMessage());
		}
	}
	
	/**
	 * 按住某键(按下某个键位并不弹起)搭配keyDown使用
	 * <li>keyCode:传入按键码
	 */
	public void keyUp(int keyCode){
		try {
			activeDm.invoke("KeyUp",keyCode);
		} catch (Exception e) {
			new Func.File().log("按键弹起异常捕获"+e.getMessage());
		}
	}
	
	/**
	 * 将java.awt.event.KeyEvent.getKeyCode转换为dm用的按键码
	 * @param keyCode java.awt.event.KeyEvent
	 * @return DM用的按键码
	 */
	public int keyCodeTransform(int keyCode){
		switch(keyCode){
			case 45:		//-
				keyCode=189;
				break;
			case 61:		//=
				keyCode=187;
				break;
			case 10:		//Enter
				keyCode=13;
				break;
			case 91:		//[
				keyCode=219;
				break;
			case 93:		//]
				keyCode=221;
				break;
			case 92:		// \
				keyCode=220;
				break;
			case 59:		// ;
				keyCode=186;
				break;
			case 44:		// ,
				keyCode=188;
				break;
			case 46:		// .
				keyCode=190;
				break;
			case 47:		// /
				keyCode=191;
				break;
		}
		return keyCode;
	}
	
	/**
	 * 将某些不能输出显示的按键为正常文字可供用户看到
	 * @param keyCode 传入按键码,传入的为dm的按键码
	 * @param oldName 原本的名字,像是enter、空格键等，此传入会失效，返回最新的
	 * @return 返回键名，如Enter、BackSpace、空格、上……等
	 */
	public String keyCodeName(int keyCode,String oldName){
		String result=null;
		switch(keyCode){
			case 27:	
				result="Esc";
				break;
			case 112:		
				result="F1";
				break;
			case 113:		
				result="F2";
				break;
			case 114:		
				result="F3";
				break;
			case 115:		
				result="F4";
				break;
			case 116:		
				result="F5";
				break;
			case 117:		
				result="F6";
				break;
			case 118:		
				result="F7";
				break;
			case 119:		
				result="F8";
				break;
			case 120:		
				result="F10";
				break;
			case 121:		
				result="F11";
				break;
			case 122:		
				result="F12";
				break;
			case 145:		
				result="Scroll Lock";
				break;
			case 8:		
				result="BackSpace";
				break;
			case 20:		
				result="Caps Lock";
				break;
			case 16:		
				result="Shift";
				break;
			case 17:		
				result="Ctrl";
				break;
			case 18:		
				result="Alt";
				break;
			case 32:		
				result="空格";
				break;
			case 38:	
				result="上";
				break;
			case 40:	
				result="下";
				break;
			case 37:	
				result="左";
				break;
			case 39:	
				result="右";
				break;
			case 13:	
				result="Enter";
				break;
			default:
				result=oldName;
				break;
		}
		return result;
	}
}
 public class Sleep {
	/**
	 * 延迟等待，多线程sleep，与软件同步停止
	 * @param millis毫秒
	 */
	public void sleep(int millis){
		
		try {
			if(millis>3000){
				int i=Math.round(millis/1000)+1;
				//include.Command.run 软件的主体控制运行状态，点击结束后此项为false
				for(int j=0;j<i&&include.Command.run;j++){
					Thread.sleep(1000);
				}
			}else{
				Thread.sleep(millis);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
 public class XinNuo {
	private ActiveXComponent activeDm=null;
	private LanAnswer lanAnswer=null;
	
	public XinNuo(ActiveXComponent activeDm) {
		this.activeDm=activeDm;
		this.lanAnswer=new LanAnswer(this.activeDm);
		verifyXinNuo();
	}
	public XinNuo(){
		verifyXinNuo();
	}
	/**
	 * 校验信诺对象
	 * 首先检查是否创建了信诺对象，若是创建了则跳过，没创建则先创建
	 */
	public void verifyXinNuo(){
		int i=0;
		while(i++<10){
			try {
				Command.activeXinNuo.invoke("WaitOnline");	//检测繁忙程度,判断信诺对象是否创建
				i=100;	//退出循环
			} catch (Exception e) {
				Command.activeXinNuo=new ActiveXComponent("XinNuo.SendData");		//创建信诺自动答题对象,再次创建
			}
		}
		if(i<100){	//小于100，循环中并没有成功过
			new Func.File().log("加载自动答题失败！可尝试关闭软件后重新打开软件");
		}
	}
	
	/**
	 * 出现验证码后的操作-自动答题
	 */
	public void autoDaTi(){
		File file=new File(this.activeDm);
		Mouse mouse=new Mouse(this.activeDm);
		Sleep sleep=new Sleep();
		
		int result=0;	//验证码结果,23为2|3两个位置
		int datiSussess=0;	//答题是否成功，进行3次答题，++递增
		while(datiSussess++<4){
			//使用信诺答题初始化
			boolean useXN=!Command.useLanAnswer;	
			
			//若是开启了局域网答题，首先使用局域网的进行答题
			//判断当前时间是否在设定的局域网答题范围内
			Calendar Cld = Calendar.getInstance();
			int hours = Cld.get(Calendar.HOUR_OF_DAY);
			int minute = Cld.get(Calendar.MINUTE);
			if(Command.hoursStart<=hours&&hours<Command.hoursEnd){
				//进行局域网答题
				if(Command.useLanAnswer){
					int handle=this.lanAnswer.faqCapture(428,368,596,460);
					String ansAll=this.lanAnswer.faqSend(Command.serverIP, handle);
					System.out.println("Lan--->"+ansAll);
					String ansArray[]=ansAll.split(":");
					
					if(ansArray[0].indexOf("Error")>-1){
						//有Error，失败了，可能是服务端关闭了
						file.log("局域网答题-Error,转而开启信诺答题");
//						useXN=true;
					}else{
						//成功了
						result=new Lang().Integer_(ansArray[1], 9);
					}
					ansArray=null;
				}
			}else{
				useXN=true;
			}
			
			
			//使用信诺进行答题
			if(useXN){
				System.out.println("使用XN答题");
				String getTid="123";
				//首先截图，将验证码截图保存为check.bmp文件
				file.screenImage("check", 428,368,596,460);
				
				try {
					Variant[] var=new Variant[5];
					var[0]=new Variant(Command.autoDaTiPasswordString);
					var[1]=new Variant(2022);	//游戏ID
					var[2]=new Variant(Command.thisFilePath+"\\"+Command.screenNoteFile+"\\check.bmp");
					var[3]=new Variant(42);	//超时时间42秒
					var[4]=new Variant("bmp");
					getTid=Command.activeXinNuo.invoke("SendFile",var).getString();
					var=null;
				} catch (Exception e) {
					file.log("自动答题SendGif异常捕获:"+e.getMessage());
				}
				
				
				int i=0;	//限制循环次数
				while(result<1&&i++<20){
					try {
						result=Integer.parseInt(Command.activeXinNuo.invoke("GetAnswer",getTid).getString());
					} catch (Exception e) {
						file.log("获取答题答案GetAnswer转int时异常捕获:"+e.getMessage());
					}
					sleep.sleep(2000);	//2秒一次请求
				}
			}
			
			//局域网答题或者XN答题的结果进行检测
			if(10<result&&result<89){
				file.log("	`e`答题成功,返回值:"+result);
				datiSussess=100;		//退出循环				
			}else{
				file.log("自动答题失败!");
				//点击刷新键重新答题
				result=0;	//失败，初始化0
				mouse.mouseClick(504,480, true);
				sleep.sleep(8000);	//8秒，避免延迟
			}
		}
		
		if(result==0){
			file.log("答题最终失败！！");
			mouse.mouseClick(561,480, true);		//点击取消键，取消自动答题
			sleep.sleep(3000);
		}else{
			String resultString=result+"";
			
			//进行验证动作，点击
			autoDaTiAction(resultString.charAt(0));
			sleep.sleep(500);
			autoDaTiAction(resultString.charAt(1));
			sleep.sleep(500);
			mouse.mouseClick(446,481, true);	//点击确定
		}
		
		file=null;
		mouse=null;
		sleep=null;
	}
	
	/**
	 * 为autoDaTi服务
	 * 8个验证码根据传入1~8进行左键点击
	 */
	public void autoDaTiAction(char param){
		int key=1;
		Mouse mouse=new Mouse(activeDm);
		try {
			key=Integer.parseInt(param+"");
		} catch (Exception e) {
			new Func.File().log("自动答题-自动填写验证动作异常捕获:"+e.getMessage());
		}
		switch (key) {
		case 1:
			mouse.mouseClick(454,390, true);
			break;
		case 2:
			mouse.mouseClick(492,390, true);
			break;
		case 3:
			mouse.mouseClick(526,392, true);
			break;
		case 4:
			mouse.mouseClick(564,392, true);
			break;
		case 5:
			mouse.mouseClick(449,429, true);
			break;
		case 6:
			mouse.mouseClick(489,432, true);
			break;
		case 7:
			mouse.mouseClick(523,430, true);
			break;
		case 8:
			mouse.mouseClick(564,430, true);
			break;
		default:
			break;
		}
		
		mouse=null;
	}
	
	/**
	 * 信诺答题，返回剩余可使用次数
	 */
	public String getAutoDaTiNum(){
		String result="查询时程序出现异常！";
		try {
			Variant[] var=new Variant[1];
			var[0]=new Variant(Command.autoDaTiPasswordString);
			String getResult=Command.activeXinNuo.invoke("SNSearch",var).getString();
			var=null;
			if(getResult.equals("-1")){
				result="上传异常";
			}else if(getResult.equals("-2")){
				result="用户名或密码错误";
			}else if(getResult.equals("-3")){
				result="查询失败";
			}else{	//查询成功，返回剩余题分
				result=getResult+"<br/>(每次验证消耗14题分)";
			}
			getResult=null;
		} catch (Exception e) {
			new Func.File().log("查询自动答题剩余时异常捕获："+e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * DM局域网答题
	 */
	public void lanDaTi(){
		
	}
}

import Func.Color;
import Func.File;
import Func.FindPic;
import Func.FindStr;
import Func.Lang;
import Func.Mouse;
import Func.Position;
import Func.Press;
import Func.Sleep;
import Func.XinNuo;
import com.jacob.activeX.ActiveXComponent;
import include.Command;

package action;



public class DaGuaiFunc {
	ActiveXComponent activeDm=null;

	Mouse mouse=null;
