var robot = require("robot-js");
var mouse = robot.Mouse();
var keyboard = robot.Keyboard();
mouse.autoDelay.max = 1;
var debug = false;
var targetColor = [0xF2361B,0xEF3519,0xEE4131,0xED4233,0xEE4031,0xCF8790,0xCF878F,0xD92411,0xDC2613 ,0xA24D53,0xA84547,0xB25C62,0xAB545B];
var pixelColor = robot.Color();
var i = 0;
var k = 0;
var data,r2,g2,b2,r1,g1,b1,s;
var searchSize = debug ? .50 : .97;
var step = 3;
var window;
var w2,h2;
var img = robot.Image();
function getWidow() {
if (robot.Window.isAxEnabled())
{
var windows = robot.Window.getList(".*Overwatch.*");
windows.map (function (w)
{
window = w;
x2 = w.getClient().x;
y2 = w.getClient().y;
w2 = w.getClient().w;
h2 = w.getClient().h;
robot.Window.setActive( window );
console.log('Overwatch window find at ' + x2 + ',' + y2 + ', size : ' + w2 + ',' + h2);
w2 = w2 / 2;
h2 = h2 / 2;
});
}
}
getWidow();
if ( window ) {
console.log('Start listen mouse Trigger');
while(1) {
s = robot.Mouse.getState();
if( s[robot.BUTTON_MID] )
bang();
}
}
function bang()
{
robot.Screen.grabScreen(img, x2 + Math.round(searchSize * w2), y2 + Math.round(searchSize * h2), Math.round((1-searchSize) * w2), Math.round((1-searchSize) * h2));
data = img.getData();
for(i = 0; i < img.getLength(); i+=step)
{
if(similar((data[i] & 0x00FFFFFF).toString(16).toUpperCase(),15) )
{
keyboard.click(robot.KEY_N);
return;
}
}
return;
}
function similar (aneighbor, atolerance)
{
atolerance = atolerance || 10;
r2 = parseInt(aneighbor.substr(0,2), 16);
g2 = parseInt(aneighbor.substr(2,2), 16);
b2 = parseInt(aneighbor.substr(4,2), 16);
for(k = 0; k < targetColor.length; k++)
{
r1 = parseInt(targetColor[k].toString(16).toUpperCase().substr(0,2), 16);
g1 = parseInt(targetColor[k].toString(16).toUpperCase().substr(2,2), 16);
b1 = parseInt(targetColor[k].toString(16).toUpperCase().substr(4,2), 16);
if (Math.abs(r1 - r2) <= atolerance && Math.abs(g1 - g2) <= atolerance && Math.abs(b1 - b2) <= atolerance)
{
return true;
}
}
return false;
}