--lua 公用函数
function abs(Aint)
	if Aint < 0 then
		Aint = - Aint;
	end
	return Aint;
end
--找找颜色
--返回1：状态，是否找到，2,找到后的X坐标，3：找到后的Y坐标
function findColor(AintBeginX, AintBeginY, AintMaxX, AintMaxY, AintR, AintG, AintB, AintDif)
	bisfind = false;
	tbResult = {};
	tbResult[0] = 0;
	for  intY = AintBeginY, AintMaxY do
		for  intX = AintBeginX, AintMaxX do
			p1 = GetPixColor(intX, intY );--获取相应点上面的颜色
			intB, intG, intR = ColorToRGB(p1); --获取点位上的颜色RGB
			--跟据RGB判断是否是自已要找的颜色
			if (abs(intB - AintB) < AintDif) and (abs(intR - AintR) < AintDif) and
				(abs(intG - AintG) < AintDif) then
				tbResult[0] = 1;
				tbResult[1] = intX;
				tbResult[2] = intY;
				bisfind = true;
				break;
			end
		end
		if bisfind then
			break;
		end
	end
	return tbResult; --返回表格
end
--逆向找颜色
--返回1：状态，是否找到，2,找到后的X坐标，3：找到后的Y坐标

function reverseFindColor(AintBeginX, AintBeginY, AintEndX, AintEndY, AintR, AintG, AintB, AintDif)
	bisfind = false;
	tbResult = {};
	tbResult[0] = 0;
	intY = AintBeginY;
	while intY > AintEndY do
		intX = AintBeginX;
		while intX > AintEndX do
			p1 = GetPixColor(intX, intY );--获取相应点上面的颜色
			intB, intG, intR = ColorToRGB(p1); --获取点位上的颜色RGB
			--WriteLog( 'intR:', intR, 'intG:',intG, 'intB:', intB );
			--跟据RGB判断是否是自已要找的颜色
			if (abs(intB - AintB) < AintDif) and (abs(intR - AintR) < AintDif) and
				(abs(intG - AintG) < AintDif) then
				tbResult[0] = 1;
				tbResult[1] = intX;
				tbResult[2] = intY;
				bisfind = true;
				break;
			else
				intX = intX - 1;
			end
		end
		if bisfind then
			break;
		else
			intY = intY - 1;
		end
	end
	return tbResult; --返回表格
end


--接任务
function recvTask()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intBeginX = intW - 210;
	intBeginY = 270;
	intMaxX = intW;
	intMaxY = 320;
	tbResult = findColor( intBeginX, intBeginY,intMaxX, intMaxY, 0, 255, 0, 1);
--	WriteLog( 'GetWindowRect()',intW,',', intH );
	if tbResult[0] == 1 then
		sendMouseClick( tbResult[1]  + 3, tbResult[2] + 5 ); --发送点击事件
		--WriteLog(  'sendMouseClick1' ,tbResult[1]  + 3, tbResult[2]  + 5 );
		return  true;
	else
		return   false;
	end
end

--完成日常任务立即前往"FFFFA0","-48|-52|00A5FF,-51|-43|00A5FF,-33|-46|00A5FF,16|-52|00FF00,24|-47|00FF00,28|-43|00FF00"
function checkGoTo()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intX = 0;
	intY = 0;
	intMaxX = intW;
	intMaxY = intH;
	strFirstClr = "FFFFA0";
	strParamter = "-48|-52|00A5FF,-51|-43|00A5FF,-33|-46|00A5FF,16|-52|00FF00,24|-47|00FF00,28|-43|00FF00";
	intResult, intX, intY  = FindMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
--	WriteLog( 'X1:', intX, 'Y:', intY, 'Result:', intResult );
	if intResult == 1 then
		sendMouseClick( intX , intY  ); --发送点击事件
		return  true;
	else
		return  false;
	end
end

--装备佩戴,回城复活 "FFFFA0","-33|-10|8C9A5E,-21|-6|697346,-23|4|374517,-24|6|404C12,-4|7|465110,26|8|343809"
function checkWear()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intX = 0;
	intY = 0;
	intMaxX = intW;
	intMaxY = intH;
	strFirstClr = "FFFFA0";
	strParamter = "-33|-10|8C9A5E,-21|-6|697346,-23|4|374517,-24|6|404C12,-4|7|465110,26|8|343809";
	intResult, intX, intY  = FindMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
--	WriteLog( 'X1:', intX, 'Y:', intY, 'Result:', intResult );
	if intResult == 1 then
		sendMouseClick( intX , intY  ); --发送点击事件
		return  true;
	else
		return  false;
	end
end



--一键出售"FFFFA0","17|2|343923,33|2|343923,33|8|343923,17|8|343923,9|8|343923,3|8|FFFFA0"
function checkyjchushou(intX, intY,intMaxX, intMaxY)
--	WriteLog( 'X2:', intX, 'Y:', intY, 'Result:', intResult );
	strFirstClr = "FFFFA0";
	strParamter = "17|2|343923,33|2|343923,33|8|343923,17|8|343923,9|8|343923,3|8|FFFFA0";
	intResult, intX, intY  = FindMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
--	WriteLog( 'X1:', intX, 'Y:', intY, 'Result:', intResult );
	if intResult == 1 then
		sendMouseClick( intX , intY  ); --发送点击事件
		return  true;
	else
		return  false;
	end
end
--一键回收"FFFFA0","17|2|343923,33|2|343923,33|8|343923,17|8|343923,9|8|343923,3|8|FFFFA0"
function checkyjhuishou(intX, intY,intMaxX, intMaxY)
--	WriteLog( 'X2:', intX, 'Y:', intY, 'Result:', intResult );
	strFirstClr = "FFFFA0";
	strParamter = "17|2|343923,33|2|343923,33|8|343923,17|8|343923,9|8|343923,3|8|FFFFA0";
	intResult, intX, intY  = FindMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
--	WriteLog( 'X1:', intX, 'Y:', intY, 'Result:', intResult );
	if intResult == 1 then
		sendMouseClick( intX + 90, intY  ); --发送点击事件
		return  true;
	else
		return  false;
	end
end
--出售
function yjcs()
	api_sendKey('66');
	api_LuaSleep(500);
	intResult,intX,intY = checkDialog();
	if intResult == 1 then
		if checkyjchushou(intX - 300, intY + 200,intX - 200, intY + 400) then
			api_LuaSleep(500);
			intResult,intX,intY = checkDialog();
			sendMouseClick( intX + 210 , intY + 340 ); --发送点击事件
		end
	end
	api_sendKey( '27' );
	api_LuaSleep(100);
	api_sendKey( '27' );
end
--回收
function yjhs()
	api_sendKey('66');
	api_LuaSleep(500);
	intResult,intX,intY = checkDialog();
	if intResult == 1 then
		if checkyjhuishou(intX - 300, intY + 200,intX - 200, intY + 400) then
			api_LuaSleep(500);
			intResult,intX,intY = checkDialog();
			sendMouseClick( intX + 210 , intY + 340 ); --发送点击事件
		end
	end
	api_sendKey( '27' );
	api_LuaSleep(100);
	api_sendKey( '27' );
end


--完成任务224,211,48
function checkOverDailyTask()
	intResult,intX,intY = checkDialog();
	if intResult == 1 then
		tbResult = findColor(intX - 300, intY,intX, intY + 220, 224, 211, 48, 1);
		if tbResult[0] == 1 then
			sendMouseClick( tbResult[1]  + 25, tbResult[2] + 3 ); --发送点击事件
			return  true;
		else
			return  false;
		end
	else
		return  false;
	end
end
--休眠(单位：秒)
function sleep(n)
	api_LuaSleep(n*1000);
end

--锁定人物"FFFFA0","1|-11|89985D,26|-11|89985D,17|-5|FFFFA0,20|3|FFFFA0,26|-3|DAE99F,33|2|000000"
function checkSuoDing()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intX = intW/2;
	intY = 10;
	intMaxX = intX + 100;
	intMaxY = 70;
	strFirstClr = "FFFFA0";
	strParamter = "1|-11|89985D,26|-11|89985D,17|-5|FFFFA0,20|3|FFFFA0,26|-3|DAE99F,33|2|000000";
	intResult, intX, intY  = api_findMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
	if intResult == 1 then
		return	true;
	else
		return	false;
	end
end
--挂机中"0101C5","0|-1|FFFFFF,6|-1|FFFFFF,6|0|0101C5,7|3|FFFFFF,19|3|FFFFFF,20|3|0101C5"
function checkHangUp()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intX = 0;
	intY = 0;
	intMaxX = intW;
	intMaxY = 120;
	strFirstClr = "0101C5";
	strParamter = "0|-1|FFFFFF,6|-1|FFFFFF,6|0|0101C5,7|3|FFFFFF,19|3|FFFFFF,20|3|0101C5";
	intResult, intX, intY  = api_findMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
	if intResult == 1 then
		return	true;
	else
		return	false;
	end
end
--自动寻路"C1FFE5","34|-2|C1FFE5,39|-2|C1FFE5,61|-1|C1FFE5,118|-4|C1FFE5,118|7|C1FFE5,158|11|C1FFE5"
function checkXunLu()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intX = 0;
	intY = 0;
	intMaxX = intW;
	intMaxY = 120;
	strFirstClr = "C1FFE5";
	strParamter = "34|-2|C1FFE5,39|-2|C1FFE5,61|-1|C1FFE5,118|-4|C1FFE5,118|7|C1FFE5,158|11|C1FFE5";
	intResult, intX, intY  = api_findMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
	if intResult == 1 then
		return	true;
	else
		return	false;
	end
end
--找黄色
function checkYellow(intBeginX, intBeginY,intMaxX, intMaxY)
	tbResult = findColor( intBeginX, intBeginY,intMaxX, intMaxY, 255, 200, 0, 1);
	--WriteLog( 'XY:', tbResult[1], 'Y:', tbResult[2], 'Result:', tbResult[0] );
	if tbResult[0] == 1 then
		sendMouseClick( tbResult[1]  + 5, tbResult[2] + 3 ); --发送点击事件
		return  true;
	else
		return  false;
	end
end
--找绿色（主线）
function checkTGreen(intBeginX, intBeginY,intMaxX, intMaxY)
	tbResult = findColor( intBeginX, intBeginY,intMaxX, intMaxY, 0, 255, 0, 1);
--	WriteLog( 'XTG:', tbResult[1], 'Y:', tbResult[2], 'Result:', tbResult[0] );
	if tbResult[0] == 1 then
		sendMouseClick( tbResult[1]  + 5, tbResult[2] + 3 ); --发送点击事件
		return  true;
	else
		return  false;
	end
end
--找绿色（日常）
function checkGreen(intBeginX, intBeginY,intMaxX, intMaxY)
	tbResult = findColor( intBeginX, intBeginY,intMaxX, intMaxY, 0, 255, 0, 1);
--	WriteLog( 'XG:', tbResult[1], 'Y:', tbResult[2], 'Result:', tbResult[0] );
	if tbResult[0] == 1 then
		sendMouseClick( tbResult[1]  + 30, tbResult[2] + 3 ); --发送点击事件
		return  true;
	else
		return  false;
	end
end
--逆向找绿色
function checkReverseGreen(AintBeginX, AintBeginY, AintEndX, AintEndY)
	tbResult = reverseFindColor(AintBeginX, AintBeginY, AintEndX, AintEndY, 0, 255, 0, 1);
--	WriteLog( 'XR:', tbResult[1], 'Y:', tbResult[2], 'Result:', tbResult[0] );
	if tbResult[0] == 1 then
		sendMouseClick( tbResult[1]  - 3, tbResult[2] - 4 ); --发送点击事件
		return  true;
	else
		return  false;
	end
end


--分别从左上和右下找绿色，并返回两点间x坐标的差
function compareGreen(intBeginX, intBeginY,intMaxX, intMaxY)
	tbResult = findColor( intBeginX, intBeginY,intMaxX, intMaxY, 0, 255, 0, 1);
	x1=tbResult[1];
	tbResultr = reverseFindColor(intMaxX, intMaxY, intBeginX, intBeginY, 0, 255, 0, 1);
	x2=tbResultr[1];
	if	tbResult[0]==1	and tbResultr[0]==1  then
		return (x2 - x1);
	else
		return 0;
	end
end


--检查对话窗"000000","0|-1|E7E7E7,0|-2|000000,0|-10|E7E7E7,0|-11|000000,-11|-10|E7E7E7,-11|-9|000000"
function checkDialog()
	intW, intH = GetWindowRect(); --获取窗体的高和宽
	intX = 0;
	intY = 0;
	intMaxX = intW;
	intMaxY = intH;
	strFirstClr = "000000";
	strParamter = "0|-1|E7E7E7,0|-2|000000,0|-10|E7E7E7,0|-11|000000,-11|-10|E7E7E7,-11|-9|000000";
	tbResult = {};
	intResult,intX,intY = FindMultiColor(intX, intY, intMaxX, intMaxY, strFirstClr, strParamter);
	return intResult,intX,intY;
end

--参数说明:  获取图片中的文字
--    参数数量:9
--    参数类型: 1:开始坐标X，开始坐标Y，目标坐标X，目标坐标Y，红R，绿G，蓝B，最大误差值Range,字宽
--    返回:字符串
    function getImageText(AintBeginX, AintBeginY, AintMaxX, AintMaxY, AintR,AintG,AintB, AintRange ,AintWordWide)
		return  GetImageText( AintBeginX, AintBeginY, AintMaxX, AintMaxY, AintR,AintG,AintB, AintRange, AintWordWide);
	end


function yjxzb()
	local file = io.open('config.txt');
	--io.output('vip/1234.txt');
	for strData1 in file:lines() do
	--	print(l);
		api_sendData_NZ(strData1);
	--	io.write(l,'\n');
		api_LuaSleep(1000);
	end
end

--取出字符串s中从i到j的字符      string.sub(s, i, j)


--将表格s连接成一个字符串        table.concat(s)
