package impsoft.nexus.bots.color.methods; import impsoft.nexus.bots.color.constants.Areas; import impsoft.nexus.bots.color.scriptobjects.RGB; import impsoft.nexus.bots.color.scriptobjects.XY; import java.awt.Rectangle; import java.awt.event.KeyEvent; public abstract class CompassMethods extends MiniMapMethods { public int getCompass() throws InterruptedException { RGB red = new RGB(159, 12, 14); XY t1 = findXYspiral( getColorArray2d(), red, Areas.COMPASS, new Rectangle[] { Areas.COMPASSEXCLUDE, Areas.COMPASSEXCLUDE2 }, 10, 10, 10); if (t1 != null) { XY t2 = findXYspiral(getColorArray2d(), red, Areas.COMPASS, new Rectangle[] { Areas.COMPASSEXCLUDE, Areas.COMPASSEXCLUDE2, new Rectangle(t1.x - 3, t1.y - 3, 6, 6) }, 10, 10, 10); if (t2 != null) { XY t3 = findXYspiral(getColorArray2d(), red, Areas.COMPASS, new Rectangle[] { Areas.COMPASSEXCLUDE, Areas.COMPASSEXCLUDE2, new Rectangle(t1.x - 3, t1.y - 3, 6, 6), new Rectangle(t2.x - 3, t2.y - 3, 6, 6) }, 10, 10, 10); if (t3 != null) { return compassHelper(t1, t2, t3); } } } warn("getCompass() can not be done"); return -1; } private int compassHelper(XY xy1, XY xy2, XY xy3) throws InterruptedException { XY CompassCenter = new XY(566, 20); xy1 = new XY(xy1.x - CompassCenter.x, CompassCenter.y - xy1.y); xy2 = new XY(xy2.x - CompassCenter.x, CompassCenter.y - xy2.y); xy3 = new XY(xy3.x - CompassCenter.x, CompassCenter.y - xy3.y); XY theMissingRedPoint = null; if (distanceDouble(xy1, xy3) > distanceDouble(xy2, xy3) && distanceDouble(xy1, xy3) > distanceDouble(xy1, xy2)) { theMissingRedPoint = xy2; } else if (distanceDouble(xy2, xy3) > distanceDouble(xy2, xy1) && distanceDouble(xy2, xy3) > distanceDouble(xy1, xy3)) { theMissingRedPoint = xy1; } else if (distanceDouble(xy1, xy2) > distanceDouble(xy2, xy3) && distanceDouble(xy1, xy2) > distanceDouble(xy1, xy3)) { theMissingRedPoint = xy3; } if (theMissingRedPoint != null) { int degrees = (int) Math.toDegrees(Math.atan2(theMissingRedPoint.y, theMissingRedPoint.x)); degrees = degreeShift(degrees, 90); return degrees; } else { //never has happened warn("compassHelper() can not be done"); return -1; } } /** * Currently accurate within 5 degrees or less. Report bugs. * * @author GenoDemon * @param degrees * @throws InterruptedException */ public void setCompass(int degrees) throws InterruptedException { setCompass(degrees, 0, 10); } public void setCompass(int degreeGoal, int attempt, int maxAttempt) throws InterruptedException { degreeGoal = degreeShift(degreeGoal, 0); attempt++; if (attempt >= maxAttempt) return; int varience = 5 * attempt / maxAttempt; int currentCompass = getCompass(); int diff = degreeDifference(degreeGoal, currentCompass); debug("[Compass Debug] Going to:" + degreeGoal + " from:" + currentCompass + " diff=" + diff); if (Math.abs(diff) > varience) { int millsecs; if (Math.abs(diff) < 5) { if (diff > 0) millsecs = random(50, 75); else millsecs = -1 * random(50, 75); } else { millsecs = diff * random(9, 14); } if (millsecs > 0) { SendKey(KeyEvent.VK_RIGHT, millsecs); sleep(millsecs + 10); } else { millsecs *= -1; SendKey(KeyEvent.VK_LEFT, millsecs); sleep(millsecs + 10); } sleep(2000); int currentCompass2 = getCompass(); int diff2 = degreeDifference(currentCompass, degreeGoal); debug("[Compass Debug] Current: " + currentCompass2 + " Want: " + degreeGoal + " Missed by: " + diff2); if (Math.abs(diff2) > varience) setCompass(degreeGoal, attempt, maxAttempt); } } public int degreeDifference(int degrees1, int degrees2) { degrees1+=360; degrees2+=360; int diff1 = degrees2-degrees1; int diff2 = 360-diff1; if(Math.abs(diff1) > Math.abs(diff2)){ return diff2; } return diff1; } public int degreeShift(int degrees, int shiftBy) { degrees += shiftBy; while (degrees <= -180) { degrees += 360; } while (degrees > 180) { degrees -= 360; } while (degrees <= -180) { degrees += 360; } while (degrees > 180) { degrees -= 360; } return degrees; } }