-
Notifications
You must be signed in to change notification settings - Fork 533
/
Scanner.js
executable file
·103 lines (75 loc) · 1.88 KB
/
Scanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var robot = require('robotjs');
// Cache screen size
var screenSize = robot.getScreenSize();
// Indexes
var X = 0;
var Y = 1;
// Create the "class" wrapper
var Scanner = {};
// Check if the given position is outside the Screen
Scanner.isOutOfBound = function (pos) {
if ( pos[X] < 0 || pos[Y] < 0 ||
pos[X] >= screenSize.width ||
pos[Y] >= screenSize.height) {
return true;
}
return false;
}
// Limits the x/y values of position to fit the screen
Scanner.makeInBounds = function (pos) {
if (pos[X] < 0) {
pos[X] = 0;
}
if (pos[X] >= screenSize.width) {
pos[X] = screenSize.width - 1;
}
if (pos[Y] < 0) {
pos[Y] = 0;
}
if (pos[Y] >= screenSize.height) {
pos[Y] = screenSize.height - 1;
}
return pos;
}
// Given start [X, Y], and a DELTA [dX, dY],
// maps from "start", adding "delta" to position,
// until "matchinColor" is found OR isOutOfBounds.
//
// If iterations reach > iterLimit:
// returns null;
//
// if isOutOfBounds:
// returns null
//
// otherwise:
// return that point
//
// Example: (X direction)
// scanUntil([0,0], [1, 0], "000000");
Scanner.scanUntil = function (start, delta, matchColor, inverted, iterLimit) {
var color, current, iterations = 0;
// (CLONE instead of using the real one)
current = Scanner.makeInBounds([start[X], start[Y]]);
if (delta[X] == 0 && delta[Y] == 0) {
return null;
}
while (!Scanner.isOutOfBound(current)) {
// Check current pixel
color = robot.getPixelColor(current[X], current[Y]);
if (!inverted && color.toString() == matchColor) {
return current;
}
if (inverted && color.toString() != matchColor) {
return current;
}
current[X] += delta[X];
current[Y] += delta[Y];
iterations++;
if (iterations > iterLimit) {
return null;
}
}
return null;
};
// Export the module
module.exports = Scanner;