Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created October 18, 2012 03:49
Show Gist options
  • Save TheBatScripts/3909769 to your computer and use it in GitHub Desktop.
Save TheBatScripts/3909769 to your computer and use it in GitHub Desktop.
DTMBuilder v1.0
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import org.veloxbot.Context;
import org.veloxbot.api.ActiveScript;
import org.veloxbot.api.Manifest;
import org.veloxbot.api.methods.Game;
import org.veloxbot.api.wrappers.DTM;
import org.veloxbot.api.wrappers.DTMBranch;
import org.veloxbot.api.wrappers.DTMRoot;
@Manifest (authors = { "TheBat,Battleguard" }, name = "DTMBuilder", version = 1.0)
public class DTMBuilder extends ActiveScript implements MouseListener{
private GUI gui = null;
private boolean OPEN = true;
public boolean onStart() {
image = Game.getImage();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new GUI();
}
});
return true;
}
public long loop() {
if(!OPEN)return -1;
return 1000;
}
private ArrayList<Rectangle> DTMloc = new ArrayList<Rectangle>();
private ArrayList<Rectangle> rects = new ArrayList<Rectangle>();
private BufferedImage cacheImage;
public void findDTMS(final int tol) {
DTMloc.clear();
final Point minPoint = new Point(800, 600);
final Point maxPoint = new Point(0, 0);
for(int i = 0; i < colorB.size(); i++) {
if(colorB.get(i).pt.x < minPoint.x) minPoint.x = colorB.get(i).pt.x;
if(colorB.get(i).pt.x > maxPoint.x) maxPoint.x = colorB.get(i).pt.x;
if(colorB.get(i).pt.y < minPoint.y) minPoint.y = colorB.get(i).pt.y;
if(colorB.get(i).pt.y > maxPoint.y) maxPoint.y = colorB.get(i).pt.y;
}
Rectangle box = new Rectangle(minPoint.x, minPoint.y, (maxPoint.x - minPoint.x) + 1, (maxPoint.y - minPoint.y) + 1);
// Check every point on screen for first color in the colorB array
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
if(checkColor(Game.getColorAt(new Point(x, y)), colorB.get(0).col, tol)) {
final Rectangle curbox = new Rectangle(x - (colorB.get(0).pt.x - box.x), y - (colorB.get(0).pt.y - box.y), box.width, box.height);
// Make sure we dont find multiple instances of same DTM
if(!DTMloc.isEmpty()) {
boolean found = false;
for(int i = 0; i < DTMloc.size(); i++) {
if(curbox.intersects(DTMloc.get(i))) found = true;
}
if(found) break;
}
if(checkColorPoints(new Point(x, y), tol)) {
DTMloc.add(curbox);
}
}
}
}
}
/**
*
* @param locM : location of point that matches the first color in the array list colorB
* @return true if all colors match
*/
private boolean checkColorPoints(Point locM, int Tol) {
try {
final Point locI = colorB.get(0).pt;
for(int i = 0; i < colorB.size(); i++) {
final Color initialColor = colorB.get(i).col;
final Point initialColorPoint = colorB.get(i).pt;
final int xshift = initialColorPoint.x - locI.x;
final int yshift = initialColorPoint.y - locI.y;
final Point pointToCheck = new Point(locM.x + xshift, locM.y + yshift);
final Color colorToCheck = Game.getColorAt(pointToCheck);
if(!checkColor(initialColor, colorToCheck, Tol)) return false;
}
return true;
} catch (Exception e) {
return false;
}
}
private boolean checkColor(final Color c, final Color c2, final int Tol) {
return (checkColor(c.getRed(), c2.getRed(), Tol)
&& checkColor(c.getGreen(), c2.getGreen(), Tol) && checkColor(c.getBlue(),
c2.getBlue(), Tol));
}
private boolean checkColor(final int RGB, final int Val, final int Tol) {
return Math.abs(RGB - Val) < Tol;
}
public void onRepaint(Graphics g) {
if(gui != null){
BufferedImage image2;
if(!consume){
ColorModel cm = image.getColorModel();
WritableRaster raster = image.copyData(null);
cacheImage = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
cm = cacheImage.getColorModel();
raster = cacheImage.copyData(null);
image2 = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
}else{
ColorModel cm = cacheImage.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = cacheImage.copyData(null);
image2 = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
onPaint1(image2.getGraphics());
gui.img.getGraphics().drawImage(image2, 0,0,image2.getWidth(),image2.getHeight(),null);
}
g.setColor(Color.GREEN);
for(int i = 0; i < rects.size(); i++){
g.drawRect(rects.get(i).x, rects.get(i).y, rects.get(i).width, rects.get(i).height);
}
}
public void onPaint1(Graphics g) {
if(curBox == null) return;
g.setColor(Color.BLACK);
g.fillRect(10, 350, 200, 45);
g.setColor(Color.WHITE);
g.drawString("x:" + curBox.loc.x +"y: " + curBox.loc.y, 20, 370);
g.drawString("r:" + curBox.rgb.getRed() +" g: " +curBox.rgb.getGreen() + " b: " + curBox.rgb.getBlue(), 20, 390);
g.setColor(curBox.rgb);
g.fillRect(10, 400, 50, 50);
for(int x = 0; x < colors.length; x++) {
for(int y = 0; y < colors.length; y++) {
ColorBox c = colors[x][y];
g.setColor(c.rgb);
g.fillRect(c.box.x, c.box.y, c.box.width, c.box.height);
}
}
g.setColor(Color.RED);
g.drawRect(colors[0][0].loc.x -1, colors[0][0].loc.y -1, 14, 14);
g.fillRect(curBox.box.x, curBox.box.y, curBox.box.width, curBox.box.height);
g.fillRect(curBox.loc.x, curBox.loc.y, 1, 1);
if(!colorB.isEmpty()) {
for(int i = 1; i < colorB.size(); i++) {
g.drawLine(colorB.get(0).pt.x, colorB.get(0).pt.y, colorB.get(i).pt.x, colorB.get(i).pt.y);
}
}
if(!DTMloc.isEmpty()) {
drawDTMSquares(g, Color.GREEN.brighter());
}
}
public void drawDTMSquares(Graphics g, final Color c) {
g.setColor(c);
for(int i = 0; i < DTMloc.size(); i++) {
Rectangle dtm = DTMloc.get(i);
g.drawRect(dtm.x, dtm.y, dtm.width, dtm.height);
g.drawString("dtm" + i, dtm.x + dtm.width - 30, dtm.y + dtm.height + 10);
}
}
private static Point loc = new Point(0, 0);
public class ColorBox {
private Color rgb;
private Point loc;
private Rectangle box;
public ColorBox(Color color, Point loc, Rectangle rectangle) {
this.rgb = color;
this.loc = loc;
this.box = rectangle;
}
public boolean checkBox(final Point cursor) {
return box.contains(cursor);
}
}
final Rectangle drawbox = new Rectangle(280, 330, 130, 130);
ColorBox[][] colors = new ColorBox[13][13];
ColorBox curBox;
Color[][] gameScache = null;
private Color[][] to2D(BufferedImage img) {
Color [][] out = new Color [img.getWidth()][img.getHeight()];
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
out [j][i] = new Color(img.getRGB(j, i));
}
}
return out;
}
//13x13
public void updateDrawBox() {
for(int y = 0; y < colors.length; y++) {
for(int x = 0; x < colors.length; x++) {
final Point curpoint = new Point(loc.x + x - 6, loc.y + y - 6);
//final Color curcolor = Game.getColorAt(curpoint);
final Color curcolor = gameScache[curpoint.x][curpoint.y];
final Rectangle currectangle = new Rectangle(280 + (x*10), 330 + (y*10), 10, 10);
colors[x][y] = new ColorBox(curcolor, curpoint, currectangle);
}
}
curBox = colors[6][6];
}
private ArrayList<JTextField> pointT = new ArrayList<JTextField>();
private ArrayList<JTextField> colorT = new ArrayList<JTextField>();
private ArrayList<JButton> selectB = new ArrayList<JButton>();
private ArrayList<JButton> removeB = new ArrayList<JButton>();
private ArrayList<ColorPoint> colorB = new ArrayList<ColorPoint>();
private BufferedImage image = null;
private boolean consume = false;
@Override
public void mouseClicked(MouseEvent e) {
loc = e.getPoint();
image = Game.getImage();
gameScache = to2D(image);
updateDrawBox();
}
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
private final JLabel headerL = new JLabel("Point & Color Grabber ", JLabel.CENTER);
private JLabel tolL = new JLabel("Color Tolerance", JLabel.RIGHT);
private JLabel radL = new JLabel("Diameter", JLabel.RIGHT);
private final JButton addB = new JButton("Add Current Color");
private final JButton clearB = new JButton("Clear All");
private final JButton copyB = new JButton("Copy all to clipboard");
private final JButton togB = new JButton("Freeze");
private final JButton findB = new JButton("Find DTMS");
private final JPanel img = new JPanel();
private JTextField tolT = new JTextField("15");
private JTextField radT = new JTextField("1");
private final Font normalFont = new Font("Book antiqua", Font.PLAIN, 16);
// GUI COMPONENTS
GridBagConstraints c;
GridBagConstraints sC;
Container pane;
Container sPane;
ScrollPane SP;
private int y;
// HELPER METHODS
// Make fonts look nice
void setFonts(Font font, Component... comps) {
for (Component curcomp : comps) {
curcomp.setFont(font);
}
}
// add borders to components
void addBorder(JLabel... lbls) {
final Border border = LineBorder.createGrayLineBorder();
for (JLabel lbl : lbls) {
lbl.setBorder(border);
}
}
// easy placement of panels
void addToGrid(GridBagConstraints c, Component comp, int gridx, int gridy, int gridwidth,
double weightx, Container cont) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.weightx = weightx;
cont.add(comp, c);
}
public JPanel getPanel(){
return img;
}
// easy placement of panels
void getSelected() {
for(int i = 0; i < pointT.size(); i++) {
}
}
void makeGUI() {
radT.setEnabled(false);
y = 0;
pane.removeAll();
SP.removeAll();
sPane.removeAll();
pane.setPreferredSize(new Dimension(image.getWidth()+20, image.getHeight() + 340));
SP.setPreferredSize(new Dimension(image.getWidth(), 175));
addToGrid(c, headerL, 0, y++, 3, 1.0, pane);
double temp1 = c.weighty;
int temp2 = c.fill;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
addToGrid(c, img, 0, y++, 3, 1.0, pane);
c.fill = temp2;
c.weighty = temp1;
addToGrid(c, addB, 0, y, 1, 1.0/3.0, pane);
addToGrid(c, clearB, 2, y, 1, 1.0/3.0, pane);
addToGrid(c, togB, 1, y++, 1, 1.0/3.0, pane);
addToGrid(c, findB, 0, y, 1, 1.0/3.0, pane);
addToGrid(c, tolL, 1, y, 1, 1.0/3.0, pane);
addToGrid(c, tolT, 2, y++, 1, 2.0/3.0, pane);
addToGrid(c, copyB, 0, y, 1, .5, pane);
addToGrid(c, radL, 1, y, 1, 1.0/3.0, pane);
addToGrid(c, radT, 2, y++, 1, 2.0/3.0, pane);
for(int i = 0; i < selectB.size(); i++) {
selectB.get(i).setText("Select Point " + i);
removeB.get(i).setText("Remove Point " + i);
pointT.get(i).setText("Point p" + i + "= new Point(" + colorB.get(i).pt.x + ", " + colorB.get(i).pt.y + ");");
colorT.get(i).setText("Color c" + i + " = new Color( " + colorB.get(i).col.getRed() + ", " + colorB.get(i).col.getGreen() + ", " + colorB.get(i).col.getBlue() + ");");
addToGrid(sC,selectB.get(i), 0, (4+ (i*2)), 1, 0.5, sPane);
addToGrid(sC,pointT.get(i), 1, (4+ (i*2)), 2, 0.5, sPane);
addToGrid(sC,removeB.get(i), 0, (5+ (i*2)), 1, 0.5, sPane);
addToGrid(sC,colorT.get(i), 1, (5+ (i*2)), 2, 0.5, sPane);
}
SP.add(sPane);
addToGrid(c, SP, 0, y, 3, 2.0/3.0, pane);
getContentPane().add(pane);
pack();
setVisible(true);
}
public GUI() {
super("Points and Color Grabber");
addBorder(headerL);
setFonts(normalFont, headerL);
pane = new Container();
pane.setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = c.ipadx = 3;
c.insets = new Insets(5, 5, 5, 5);
SP = new ScrollPane();
sPane = new Container();
sPane.setLayout(new GridBagLayout());
sC = new GridBagConstraints();
sC.fill = GridBagConstraints.HORIZONTAL;
sC.ipady = c.ipadx = 3;
sC.insets = new Insets(5, 5, 5, 5);
makeGUI();
setLocationRelativeTo(getOwner());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
OPEN = false;
}
});
clearB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DTMloc.clear();
selectB.clear();
removeB.clear();
pointT.clear();
colorT.clear();
colorB.clear();
rects.clear();
makeGUI();
}
});
togB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
consume = !consume;
if(consume){
togB.setText("UnFreeze");
}else{
togB.setText("Freeze");
}
}
});
findB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DTMRoot DTM_PT = new DTMRoot(colorB.get(0).col, Integer.parseInt(tolT.getText()));
DTMBranch [] DTM_PTS = getPoints();
DTM dtm = new DTM(DTM_PT, DTM_PTS);
drawDTMS(dtm, img.getGraphics());
}
private void drawDTMS(DTM dtm, Graphics g) {
Point [] points = dtm.getAll(new Rectangle(0, 0, image.getWidth(), image.getHeight()));
rects = new ArrayList<Rectangle>();
Context.console.log(points.length);
for(int i = 0; i < points.length; i++){
rects.add(buildRect(dtm, points[i]));
}
}
private Rectangle buildRect(DTM dtm, Point pt) {
Point [] points = new Point [dtm.getBranches().length+1];
points [0] = pt;
for(int i = 0; i < dtm.getBranches().length; i++){
points [i+1] = new Point(pt.x + dtm.getBranches()[i].getOffsetX(),pt.y + dtm.getBranches()[i].getOffsetY());
}
int minX = points[0].x;
for(int i = 0; i < points.length; i++){
if(points[i].x < minX){
minX = points[i].x;
}
}
int maxX = points[0].x;
for(int i = 0; i < points.length; i++){
if(points[i].x > maxX){
maxX = points[i].x;
}
}
int minY = points[0].y;
for(int i = 0; i < points.length; i++){
if(points[i].y < minY){
minY = points[i].y;
}
}
int maxY = points[0].y;
for(int i = 0; i < points.length; i++){
if(points[i].y > maxY){
maxY = points[i].y;
}
}
return new Rectangle(minX,minY,maxX-minX, maxY-minY);
}
private DTMBranch[] getPoints() {
try{
ArrayList<DTMBranch> points = new ArrayList<DTMBranch>();
for(int i = 1; i < colorB.size(); i++){
Point pt = new Point(colorB.get(i).pt.x-colorB.get(0).pt.x,colorB.get(i).pt.y-colorB.get(0).pt.y);
points.add(new DTMBranch(colorB.get(i).col, pt.x,pt.y,Integer.parseInt(tolT.getText())));
}
return points.toArray(new DTMBranch[points.size()]);
}catch(IndexOutOfBoundsException ex){
return new DTMBranch[0];
}
}
});
copyB.addActionListener(new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
int tol = Integer.parseInt(tolT.getText());
int dia = Integer.parseInt(radT.getText());
//TODO change these
String mainPoint = "DTMRoot DTM_PT_"+ count+" = new DTMRoot(new Color(" + colorB.get(0).col.getRed() + ", " + colorB.get(0).col.getGreen() + ", " + colorB.get(0).col.getBlue() + "), new Tolerance("+ tol + ", " + tol + ", " + tol+"))";
String subPoints = "DTMBranch [] DTM_PTS_"+ count+" = {";
for(int i = 1; i < pointT.size(); i++) {
subPoints += " new DTMBranch(new ColourPoint(new Point(" + (colorB.get(i).pt.x-colorB.get(0).pt.x) + ", " + (colorB.get(i).pt.y-colorB.get(0).pt.y) +"), new Color( " + colorB.get(i).col.getRed() + ", " + colorB.get(i).col.getGreen() + ", " + colorB.get(i).col.getBlue() + ")), new Tolerance("+tol + ", " + tol + ", " + tol+"),"+dia+"),";
}
subPoints = subPoints.substring(0, subPoints.length() - 1);
mainPoint += ";";
subPoints += "};";
String finalS = mainPoint + "\n" + subPoints + "\nDTM dtm"+count+" = new DTM(DTM_PT_"+ count+", DTM_PTS_"+ (count++)+");";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
StringSelection strSel = new StringSelection(finalS);
clipboard.setContents(strSel, null);
}
});
img.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
loc = e.getPoint();
if(drawbox.contains(loc)) {
for(int y = 0; y < colors.length; y++) {
for(int x = 0; x < colors.length; x++) {
if(colors[x][y].checkBox(loc)) {
loc = colors[x][y].loc;
break;
}
}
}
} else {
gameScache = consume?to2D(cacheImage):to2D(image);
}
updateDrawBox();
}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
});
addB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
colorB.add(new ColorPoint(curBox.loc,curBox.rgb));
selectB.add(new JButton());
removeB.add(new JButton());
pointT.add(new JTextField());
colorT.add(new JTextField());
selectB.get(selectB.size() - 1).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < selectB.size(); i++) {
if(e.getSource().equals(selectB.get(i))) {
loc = colorB.get(i).pt;
updateDrawBox();
}
}
}
});
removeB.get(removeB.size() - 1).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < removeB.size(); i++) {
if(e.getSource().equals(removeB.get(i))) {
colorB.remove(i);
selectB.remove(i);
removeB.remove(i);
pointT.remove(i);
colorT.remove(i);
makeGUI();
}
}
}
});
makeGUI();
}catch(Exception ex){}
}
});
}
}
private class ColorPoint{
Point pt;
Color col;
public ColorPoint(Point pt, Color col){
this.pt = pt;
this.col = col;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment