rendered paste bodypackage impsoft.nexus.bots.color.scriptobjects;import impsoft.nexus.bots.color.constants.Areas;import impsoft.nexus.bots.color.methods.*;import impsoft.nexus.bots.input.InputBot;import impsoft.nexus.bots.input.Order;import impsoft.nexus.bots.input.threads.InputThread;import java.awt.Color;import java.awt.Rectangle;import java.util.ArrayList;public class RGBCluster{ public void setNumberOfHorizontalSearchZones(int x) { theNumberOfHorizontalSearchZones = x; updateSettings(); } public RGB[] getRGBs() { return theRGBs; } public void setNumberOfVerticalSearchZones(int y) { theNumberOfVerticalSearchZones = y; updateSettings(); } private void updateSettings() { zoneDx = (float)bounds.width / (float)theNumberOfHorizontalSearchZones; zoneDy = (float)bounds.height / (float)theNumberOfVerticalSearchZones; } public void setBounds(Rectangle bounds) { this.bounds = bounds; updateSettings(); } public Rectangle getBounds(Rectangle bounds) { return bounds; } public RGBCluster(RGB rgbs[], GameScreenMethods methods) { this(rgbs, new Rectangle(Areas.GAME.x, Areas.GAME.y + 30, Areas.GAME.width, Areas.GAME.height - 30), methods); } public RGBCluster(RGB rgbs[], Rectangle bounds, GameScreenMethods methods) { thePoints = new XY[0]; matchesAtAPoint = new int[0]; hasPointBeenCompleted = new boolean[0]; paintSearchZones = true; movingAccuracy = 10; minPixleCountInBox = 1; theRGBs = rgbs; theScript = methods; theNumberOfHorizontalSearchZones = 15; theNumberOfVerticalSearchZones = 10; setBounds(bounds); } public XY[] getPoints() { return thePoints; } public void gatherNewPoints(int points) throws InterruptedException { int tolerances[][] = getToleraces(); boolean filterThoseNearBy = true; long start = System.currentTimeMillis(); short colorMap[][][] = theScript.getColorArray2d(); int counts[][][] = new int[theNumberOfHorizontalSearchZones][theNumberOfVerticalSearchZones][theRGBs.length]; int totalColorCounts[] = new int[theRGBs.length]; for(int x = 0; x < theNumberOfHorizontalSearchZones; x++) { for(int y = 0; y < theNumberOfVerticalSearchZones; y++) { int xS = bounds.x + x * (int)zoneDx; int yS = bounds.y + y * (int)zoneDy; for(int k = 0; k < theRGBs.length; k++) { int count = StaticColorMethods.numRGBs(colorMap, theRGBs[k], xS, yS, xS + (int)zoneDx, yS + (int)zoneDy, tolerances[k][0], tolerances[k][1], tolerances[k][2]); counts[x][y][k] = count; totalColorCounts[k] += count; } } } float actualZones[][] = new float[theNumberOfHorizontalSearchZones - 1][theNumberOfVerticalSearchZones - 1]; float rgbWeight = 1.0F / (float)theRGBs.length; for(int x = 0; x < theNumberOfHorizontalSearchZones - 1; x++) { for(int y = 0; y < theNumberOfVerticalSearchZones - 1; y++) { for(int k = 0; k < theRGBs.length; k++) if(totalColorCounts[k] > 0) { int colorCount = 0; colorCount += counts[x][y][k]; colorCount += counts[x + 1][y][k]; colorCount += counts[x][y + 1][k]; colorCount += counts[x + 1][y + 1][k]; if(colorCount >= minPixleCountInBox) actualZones[x][y] += (rgbWeight * (float)colorCount) / (float)totalColorCounts[k]; } } } boolean selected[][] = new boolean[theNumberOfHorizontalSearchZones - 1][theNumberOfVerticalSearchZones - 1]; XY result[] = new XY[points]; matchesAtAPoint = new int[points]; hasPointBeenCompleted = new boolean[points]; for(int i = 0; i < points; i++) { int winnerX = -1; int winnerY = -1; double winner = 0.0D; for(int x = 0; x < theNumberOfHorizontalSearchZones - 1; x++) { for(int y = 0; y < theNumberOfVerticalSearchZones - 1; y++) if(!selected[x][y] && (double)actualZones[x][y] > winner) { winnerX = x; winnerY = y; winner = actualZones[x][y]; } } if(winnerX == -1 || winnerY == -1 || winner == 0.0D) { XY newResult[] = new XY[i]; System.arraycopy(result, 0, newResult, 0, i); result = newResult; break; } selected[winnerX][winnerY] = true; if(filterThoseNearBy) { boolean innerXinRange = winnerX - 1 != -1; boolean outerXinRange = winnerX + 1 != theNumberOfHorizontalSearchZones - 1; boolean outerYinRange = winnerY + 1 != theNumberOfVerticalSearchZones - 1; boolean innerYinRange = winnerY - 1 != -1; if(outerXinRange) selected[winnerX + 1][winnerY] = true; if(innerXinRange) selected[winnerX - 1][winnerY] = true; if(outerYinRange) selected[winnerX][winnerY + 1] = true; if(innerYinRange) selected[winnerX][winnerY - 1] = true; } int minX = bounds.x + (int)(zoneDx * (float)winnerX); int minY = bounds.y + (int)(zoneDy * (float)winnerY); int maxX = bounds.x + (int)(zoneDx * (float)(winnerX + 2)); int maxY = bounds.y + (int)(zoneDy * (float)(winnerY + 2)); int interval = 6; int smallerWinnerX = -1; int smallerWinnerY = -1; int smallerWinnerCount = -1; for(int x = minX; x < maxX; x += 6) { for(int y = minY; y < maxY; y += 6) { int count = 0; for(int k = 0; k < theRGBs.length; k++) count += StaticColorMethods.numRGBs(colorMap, theRGBs[k], x, y, x + 6, y + 6, tolerances[k][0], tolerances[k][1], tolerances[k][2]); if(count > smallerWinnerCount) { smallerWinnerX = x + 3; smallerWinnerY = y + 3; smallerWinnerCount = count; } } } if(paintSearchZones) theScript.paintRectangle(new Rectangle(minX, minY, maxX - minX, maxY - minY), new Color(0, 0, 0, 63), 3000); result[i] = new XY(smallerWinnerX, smallerWinnerY); matchesAtAPoint[i] = smallerWinnerCount; } thePoints = result; if(paintSearchZones) paintPoints(4000, Color.red, 1); theScript.debug((new StringBuilder("findRGBsCluster() took")).append(System.currentTimeMillis() - start).toString()); } public boolean doMenu(String action) throws InterruptedException { return doCluster(0, 0, null, action, false); } public boolean doTopTextAndMenu(String topText, String action) throws InterruptedException { return doCluster(0, 0, topText, action, false); } public boolean doMenuOnMoving(String action) throws InterruptedException { return doCluster(0, 0, null, action, true); } public boolean doTopTextAndMenuOnMoving(String topText, String action) throws InterruptedException { return doCluster(0, 0, topText, action, true); } public boolean doCluster(int xOffet, int yOffset, String topText, String action, boolean moving) throws InterruptedException { return doClusterAndReturnPointClicked(xOffet, yOffset, topText, action, moving) != null; } public XY doClusterAndReturnPointClicked(int xOffet, int yOffset, String topText, String action, boolean moving) throws InterruptedException { removeRedundantPoints(10); sortPointsByDistanceToXY(new XY((int)bounds.getCenterX(), (int)bounds.getCenterY())); XY nextPoint; while((nextPoint = getNextIncompletePoint()) != null) { long mousetime = 0L; XY movingMouseTo = new XY(nextPoint.x + xOffet, nextPoint.y + yOffset); theScript.addOrder(new Order(theScript.getPrority(), 5, movingMouseTo.x, movingMouseTo.y, Areas.ENTIRE, false)); do { mousetime = theScript.getInputBot().theInputTread.timeTillDone(); if(mousetime != -1L) if(moving) { int interval = 300; updatePoints(); removeRedundantPoints(3); if(mousetime > (long)interval) { paintPoints(interval); Thread.sleep(interval); } else { paintPoints((int)mousetime); Thread.sleep(mousetime); } nextPoint = getNextIncompletePoint(); if(nextPoint != null) { XY updatedMovingMouseTo = new XY(nextPoint.x + xOffet, nextPoint.y + yOffset); if(SortMethods.distance(movingMouseTo, updatedMovingMouseTo) > movingAccuracy) { movingMouseTo = updatedMovingMouseTo; theScript.addOrder(new Order(theScript.getPrority(), 5, movingMouseTo.x, movingMouseTo.y, Areas.ENTIRE, true)); Thread.sleep(10L); } } } else { paintPoints((int)mousetime); Thread.sleep(mousetime); } } while(mousetime > 10L); if(nextPoint != null) { boolean click = topText == null; for(int i = 0; !click && i < 2; i++) { click = theScript.getTopText().contains(topText); if(!click) Thread.sleep(StaticColorMethods.defaultmaxAgeInMillisecs + 1); } if(click) if(action == null) { theScript.mouseClickLeft(new XY(nextPoint.x + xOffet, nextPoint.y + yOffset)); } else { theScript.mouseClickRight(new XY(nextPoint.x + xOffet, nextPoint.y + yOffset)); Menu menu = theScript.getMenu(Areas.GAME, 2000); if(menu != null) { if(menu.selectContains(action)) { removeAllButNextIncompletePoint(); Timer t = new Timer(20000L); do { paintPoints(StaticColorMethods.defaultmaxAgeInMillisecs * 3); updatePoints(); Thread.sleep(StaticColorMethods.defaultmaxAgeInMillisecs); updatePoints(); Thread.sleep(StaticColorMethods.defaultmaxAgeInMillisecs); updatePoints(); } while((theScript.isMoving() || theScript.isMapFlagUp()) && !t.isUp()); return getNextIncompletePoint(); } menu.exit(); } } completeLastPoint(); } } return null; } private XY completeLastPoint() { for(int i = 0; i < thePoints.length; i++) if(!hasPointBeenCompleted[i]) { hasPointBeenCompleted[i] = true; return thePoints[i]; } return null; } private void removeAllButNextIncompletePoint() { XY newPoints[] = new XY[1]; int newmatchesAtAPoint[] = new int[1]; boolean newhasPointBeenCompleted[] = new boolean[1]; for(int i = 0; i < thePoints.length; i++) { if(hasPointBeenCompleted[i]) continue; newPoints[0] = thePoints[i]; newmatchesAtAPoint[0] = matchesAtAPoint[i]; break; } thePoints = newPoints; matchesAtAPoint = newmatchesAtAPoint; hasPointBeenCompleted = newhasPointBeenCompleted; } private XY getNextIncompletePoint() { for(int i = 0; i < thePoints.length; i++) if(!hasPointBeenCompleted[i]) return thePoints[i]; return null; } public void sortPointsByDistanceToXY(XY location) { ArrayList result = new ArrayList(); ArrayList ints = new ArrayList(); ArrayList bools = new ArrayList(); for(int i = 0; i < thePoints.length; i++) { int indexClosest = -1; int Closestdistance = 0; for(int j = 0; j < thePoints.length; j++) if(thePoints[j] != null) { int distance = SortMethods.distance(thePoints[j], location); if(indexClosest == -1 || Closestdistance > distance) { Closestdistance = distance; indexClosest = j; } } if(indexClosest != -1) { result.add(thePoints[indexClosest]); ints.add(Integer.valueOf(matchesAtAPoint[indexClosest])); bools.add(Boolean.valueOf(hasPointBeenCompleted[indexClosest])); thePoints[indexClosest] = null; } } thePoints = new XY[result.size()]; matchesAtAPoint = new int[result.size()]; hasPointBeenCompleted = new boolean[result.size()]; for(int i = 0; i < thePoints.length; i++) { thePoints[i] = (XY)result.get(i); matchesAtAPoint[i] = ((Integer)ints.get(i)).intValue(); hasPointBeenCompleted[i] = ((Boolean)bools.get(i)).booleanValue(); } } public void removePointsNearColorUsingAutoTolerance(RGB rgb) throws InterruptedException { int redTolerance = GameScreenMethods.getGameScreenTolerance(rgb.r); int greenTolerance = GameScreenMethods.getGameScreenTolerance(rgb.g); int blueTolerance = GameScreenMethods.getGameScreenTolerance(rgb.b); removePointsNearColor(rgb, redTolerance, greenTolerance, blueTolerance); } public void removePointsNearColor(RGB rgb, int redTolerance, int greenTolerance, int blueTolerance) throws InterruptedException { int size = 100; ArrayList result = new ArrayList(); ArrayList ints = new ArrayList(); ArrayList bools = new ArrayList(); short colorMap[][][] = theScript.getColorArray2d(); for(int i = 0; i < thePoints.length; i++) { Rectangle zone = new Rectangle(thePoints[i].x - size / 2, thePoints[i].y - size / 2, size, size); zone = zone.intersection(bounds); int count = StaticColorMethods.numRGBs(colorMap, rgb, zone, redTolerance, greenTolerance, blueTolerance); if(count == 0) { result.add(thePoints[i]); ints.add(Integer.valueOf(matchesAtAPoint[i])); bools.add(Boolean.valueOf(hasPointBeenCompleted[i])); } } thePoints = new XY[result.size()]; matchesAtAPoint = new int[result.size()]; hasPointBeenCompleted = new boolean[result.size()]; for(int i = 0; i < thePoints.length; i++) { thePoints[i] = (XY)result.get(i); matchesAtAPoint[i] = ((Integer)ints.get(i)).intValue(); hasPointBeenCompleted[i] = ((Boolean)bools.get(i)).booleanValue(); } } public void removeRedundantPoints(int distanceToEach) { ArrayList result = new ArrayList(); ArrayList ints = new ArrayList(); ArrayList bools = new ArrayList(); for(int i = 0; i < thePoints.length; i++) { boolean valid = true; for(int j = 0; j < i; j++) { if(SortMethods.distance(thePoints[i], thePoints[j]) >= distanceToEach) continue; valid = false; break; } if(valid) { result.add(thePoints[i]); ints.add(Integer.valueOf(matchesAtAPoint[i])); bools.add(Boolean.valueOf(hasPointBeenCompleted[i])); } } thePoints = new XY[result.size()]; matchesAtAPoint = new int[result.size()]; hasPointBeenCompleted = new boolean[result.size()]; for(int i = 0; i < thePoints.length; i++) { thePoints[i] = (XY)result.get(i); matchesAtAPoint[i] = ((Integer)ints.get(i)).intValue(); hasPointBeenCompleted[i] = ((Boolean)bools.get(i)).booleanValue(); } } private int[][] getToleraces() { int tolerances[][] = new int[theRGBs.length][3]; for(int i = 0; i < theRGBs.length; i++) { tolerances[i][0] = GameScreenMethods.getGameScreenTolerance(theRGBs[i].r); tolerances[i][1] = GameScreenMethods.getGameScreenTolerance(theRGBs[i].g); tolerances[i][2] = GameScreenMethods.getGameScreenTolerance(theRGBs[i].b); } return tolerances; } public void updatePoints() throws InterruptedException { int tolerances[][] = getToleraces(); int minXGame = bounds.x; int minYGame = bounds.y; int maxXGame = bounds.x + bounds.width; int maxYGame = bounds.y + bounds.height; short colorMap[][][] = theScript.getColorArray2d(); for(int i = 0; i < thePoints.length; i++) { boolean done = false; for(int size = 1; size < 5 && !done; size++) { int minX = (int)((float)thePoints[i].x - ((float)size * zoneDx) / 2.0F); int minY = (int)((float)thePoints[i].y - ((float)size * zoneDy) / 2.0F); int maxX = (int)((float)thePoints[i].x + ((float)size * zoneDx) / 2.0F); int maxY = (int)((float)thePoints[i].y + ((float)size * zoneDy) / 2.0F); if(minX < minXGame) minX = minXGame; if(minY < minYGame) minY = minYGame; if(maxX > maxXGame) maxX = maxXGame; if(maxY > maxYGame) maxY = maxYGame; int interval = 6; int smallerWinnerX = -1; int smallerWinnerY = -1; int smallerWinnerCount = -1; for(int x = minX; x < maxX; x += 6) { for(int y = minY; y < maxY; y += 6) { int count = 0; for(int k = 0; k < theRGBs.length; k++) count += StaticColorMethods.numRGBs(colorMap, theRGBs[k], x, y, x + 6, y + 6, tolerances[k][0], tolerances[k][1], tolerances[k][2]); if(count > smallerWinnerCount) { smallerWinnerX = x + 3; smallerWinnerY = y + 3; smallerWinnerCount = count; } } } if((smallerWinnerCount * 3) / 2 >= matchesAtAPoint[i]) { done = true; thePoints[i] = new XY(smallerWinnerX, smallerWinnerY); matchesAtAPoint[i] = smallerWinnerCount; } } } } public void paintPoints(int timeToPaint, Color color, int size) { Color theColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), 127); for(int i = 0; i < thePoints.length; i++) if(!hasPointBeenCompleted[i]) { XY spot = thePoints[i]; theScript.paintRectangle(new Rectangle(spot.x - size, spot.y - size, size * 2, size * 2), theColor, timeToPaint); } } public void paintPoints(int timeToPaint) { paintPoints(timeToPaint, Color.GREEN, 3); } protected Rectangle bounds; protected RGB theRGBs[]; protected GameScreenMethods theScript; protected XY thePoints[]; protected int matchesAtAPoint[]; protected boolean hasPointBeenCompleted[]; protected float zoneDx; protected float zoneDy; protected int theNumberOfHorizontalSearchZones; protected int theNumberOfVerticalSearchZones; public boolean paintSearchZones; public int movingAccuracy; public int minPixleCountInBox;}