Skip to content

Instantly share code, notes, and snippets.

@CalebWhiting
Last active November 24, 2016 22:46
Show Gist options
  • Save CalebWhiting/5963673c379f3f8e890c to your computer and use it in GitHub Desktop.
Save CalebWhiting/5963673c379f3f8e890c to your computer and use it in GitHub Desktop.
DTM
import org.dang.api.script.wrappers.DTMCentral;
import org.dang.api.script.wrappers.DTMSubPoint;
import org.runedream.api.methods.Game;
import org.runedream.api.methods.ImageUtil;
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;
}
public Point getRotated(final Rectangle bounds) {
final BufferedImage screen = Game.getImage();
for(int x=0; x<360;x++) {
final BufferedImage rotated = ImageUtil.rotate(screen, x);
final Point[] points = getColor(getCentral().getColor(), getCentral().getTolerance(), rotated, bounds);
for(final Point p : points) {
if(isAt(p, rotated)) {
return p;
}
}
}
return null;
}
private boolean isAt(final Point p, final BufferedImage image) {
for (final DTMSubPoint sub : subPoints) {
if (!sub.isAt(p, image)) {
return false;
}
}
return true;
}
public Point[] getColor(final Color color, final int tolerance, final BufferedImage image, final Rectangle bounds) {
final List<Point> points = new LinkedList<Point>();
final int width = image.getWidth(), height = image.getHeight();
for(int x = bounds.x; x < bounds.x + width; x++) {
for(int y = bounds.y; y < bounds.y + height; y++) {
if(isColor(color, new Color(image.getRGB(x, y)), tolerance)) {
points.add(new Point(x, y));
}
}
}
return points.toArray(new Point[points.size()]);
}
/**
*
* @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 (isAt(point)) {
points.add(point);
}
}
return points.toArray(new Point[points.size()]);
}
/**
*
* @param x the X point of the bounds to search within
* @param y the Y point of the bounds to search within
* @param width the width of the bounds to search within
* @param height the height of the bounds to search within
* @return an array of Points where this is found, within the bounds specified.
*/
public Point[] getAll(final int x, final int y, final int width, final int height) {
return getAll(new Rectangle(x, y, width, height));
}
/**
*
* @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(getCentral().getColor(), bound, getCentral().getTolerance());
for (final Point point : points_of_color) {
if (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 central;
}
/**
*
* @return the DTMSubPoints of this
*/
public DTMSubPoint[] getSubPoints() {
return 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