Last active
November 24, 2016 22:43
-
-
Save CalebWhiting/8c73132a03a88d5dfaf9 to your computer and use it in GitHub Desktop.
DTM Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.dang.api.script.wrappers.DTMCentral; | |
import org.dang.api.script.wrappers.DTMSubPoint; | |
import org.runedream.api.methods.Game; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* @author Caleb Date: 28/03/12 Time: 14:14 | |
*/ | |
public class DTM { | |
/** | |
* | |
* @param central the DTMCentral as a string | |
* @param subs the DTMSubPoints as a string | |
* @return the Strings to a DTM | |
*/ | |
public static DTM fromString(final String central, final String... subs) { | |
final DTMCentral c = DTMCentral.fromString(central); | |
final List<DTMSubPoint> s = new LinkedList<DTMSubPoint>(); | |
for (final String sub : subs) { | |
s.add(DTMSubPoint.fromString(sub)); | |
} | |
return new DTM(c, s.toArray(new DTMSubPoint[s.size()])); | |
} | |
private final DTMCentral central; | |
private final DTMSubPoint[] subPoints; | |
private final Rectangle GAME_SCREEN = new Rectangle(0, 0, Game.getCanvasSize().width, Game.getCanvasSize().height); | |
/** | |
* | |
* @param central the DTMCentral as a string | |
* @param subs the DTMSubPoints as a string | |
*/ | |
public DTM(final String central, final String... subs) { | |
final DTM dtm = fromString(central, subs); | |
this.central = dtm.getCentral(); | |
this.subPoints = dtm.getSubPoints(); | |
} | |
/** | |
* | |
* @param central the DTMCentral of this | |
* @param subPoints the DTMSubPoints of this | |
*/ | |
public DTM(final DTMCentral central, final DTMSubPoint... subPoints) { | |
this.central = central; | |
this.subPoints = subPoints; | |
} | |
/** | |
* | |
* @param bound the bounds to search within for this | |
* @return an array of Points where this is found | |
*/ | |
public Point[] getAll(final Rectangle bound) { | |
final List<Point> points = new LinkedList<Point>(); | |
final List<Point> points_of_color = getColor(this.getCentral().getColor(), bound, this.getCentral().getTolerance()); | |
for (final Point point : points_of_color) { | |
if (this.isAt(point)) { | |
points.add(point); | |
} | |
} | |
return points.toArray(new Point[points.size()]); | |
} | |
/** | |
* | |
* @return an array of Points where this is found | |
*/ | |
public Point[] getAll() { | |
return getAll(GAME_SCREEN); | |
} | |
/** | |
* | |
* @param bound the bounds to search within for this | |
* @return where this is found, null if none | |
*/ | |
public Point get(final Rectangle bound) { | |
final List<Point> points_of_color = getColor(this.getCentral().getColor(), bound, this.getCentral().getTolerance()); | |
for (final Point point : points_of_color) { | |
if (this.isAt(point)) { | |
return point; | |
} | |
} | |
return null; | |
} | |
/** | |
* | |
* @return where this is found, null if none | |
*/ | |
public Point get() { | |
return get(GAME_SCREEN); | |
} | |
/** | |
* | |
* @param bound the bounds to search within | |
* @return if this is found | |
*/ | |
public boolean isValid(final Rectangle bound) { | |
return this.get(bound) != null; | |
} | |
/** | |
* | |
* @return if this is found | |
*/ | |
public boolean isValid() { | |
return isValid(GAME_SCREEN); | |
} | |
/** | |
* | |
* @param point the point to check | |
* @return if this is found here | |
*/ | |
private boolean isAt(final Point point) { | |
for (final DTMSubPoint sub : subPoints) { | |
if (!sub.isAt(point)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* | |
* @return the DTMCentral of this | |
*/ | |
public DTMCentral getCentral() { | |
return this.central; | |
} | |
/** | |
* | |
* @return the DTMSubPoints of this | |
*/ | |
public DTMSubPoint[] getSubPoints() { | |
return this.subPoints; | |
} | |
/** | |
* | |
* | |
* @param color Color to search for | |
* @param toSearch the rectangle to search within | |
* @param tolerance the tolerance to allow for the color | |
* @return all points that of color "color" | |
*/ | |
private static List<Point> getColor(final Color color, final Rectangle toSearch, int tolerance) { | |
final List<Point> points = new LinkedList<Point>(); | |
final BufferedImage image = Game.getImage(); | |
for (int x = toSearch.x; x < toSearch.x + toSearch.width; x++) { | |
for (int y = toSearch.y; y < toSearch.y + toSearch.height; y++) { | |
if (isColor(new Color(image.getRGB(x, y)), color, tolerance)) { | |
points.add(new Point(x, y)); | |
} | |
} | |
} | |
return points; | |
} | |
/** | |
* | |
* @param colorOne the first color to compare | |
* @param colorTwo the color to compare 'colorOne' to | |
* @param tolerance the tolerance to allow between each color | |
* @return if the color's RGS values are within the tolerance given | |
*/ | |
public static boolean isColor(final Color colorOne, final Color colorTwo, final int tolerance) { | |
final int[] one = {colorOne.getRed(), colorOne.getGreen(), colorOne.getBlue()}; | |
final int[] two = {colorTwo.getRed(), colorTwo.getGreen(), colorTwo.getBlue()}; | |
return isWithinTolerance(one[0], two[0], tolerance) && isWithinTolerance(one[1], two[1], tolerance) && isWithinTolerance(one[2], two[2], tolerance); | |
} | |
/** | |
* | |
* @param num the first number to compare | |
* @param num2 the number to compare to 'num' | |
* @param tolerance the amount to allow between the values | |
* @return if num and num2 are within the given tolerance of each other | |
*/ | |
public static boolean isWithinTolerance(final int num, final int num2, final int tolerance) { | |
return num <= num2 + tolerance && num >= num2 - tolerance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment