Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created June 11, 2012 02:00
Show Gist options
  • Save TheBatScripts/2908088 to your computer and use it in GitHub Desktop.
Save TheBatScripts/2908088 to your computer and use it in GitHub Desktop.
DTMBuilder TriBot 2.1
package scripts;
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.KeyEvent;
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.tribot.api.colour.DTMs;
import org.tribot.api.colour.Screen;
import org.tribot.api.colour.types.ColourPoint;
import org.tribot.api.colour.types.DTM;
import org.tribot.api.colour.types.DTMPoint;
import org.tribot.api.colour.types.DTMSubPoint;
import org.tribot.api.colour.types.Tolerance;
import org.tribot.bot.ClientContext;
import org.tribot.script.Script;
import org.tribot.script.ScriptManifest;
import org.tribot.script.interfaces.EventBlockingOverride;
import org.tribot.script.interfaces.Painting;
@ScriptManifest (authors = { "TheBat,Battleguard" }, category = "Tools", name = "DTMBuilder")
public class DTMBuilder extends Script implements Painting, EventBlockingOverride{
private GUI gui = null;
private boolean OPEN = true;
public boolean onStart() {
image = ClientContext.get().imagePanel.M;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new GUI();
}
});
return true;
}
public int loop() {
if(!OPEN)return -1;
return 1000;
}
private ArrayList<Rectangle> DTMloc = new ArrayList<Rectangle>();
private ArrayList<Rectangle> rects = new ArrayList<Rectangle>();
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).point.x < minPoint.x) minPoint.x = colorB.get(i).point.x;
if(colorB.get(i).point.x > maxPoint.x) maxPoint.x = colorB.get(i).point.x;
if(colorB.get(i).point.y < minPoint.y) minPoint.y = colorB.get(i).point.y;
if(colorB.get(i).point.y > maxPoint.y) maxPoint.y = colorB.get(i).point.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 < Screen.getDimension().width; x++) {
for(int y = 0; y < Screen.getDimension().getHeight(); y++) {
if(checkColor(Screen.getColourAt(new Point(x, y)), colorB.get(0).colour, tol)) {
final Rectangle curbox = new Rectangle(x - (colorB.get(0).point.x - box.x), y - (colorB.get(0).point.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).point;
for(int i = 0; i < colorB.size(); i++) {
final Color initialColor = colorB.get(i).colour;
final Point initialColorPoint = colorB.get(i).point;
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 = Screen.getColourAt(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 onPaint(Graphics g) {
if(gui != null){
ColorModel cm = image.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = image.copyData(null);
BufferedImage image2 = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
onPaint1(image2.getGraphics());
gui.img.getGraphics().drawImage(image2, 0,0,image2.getWidth(),image2.getHeight(),null);
}
if(consume)g.setColor(Color.RED);
else g.setColor(Color.GREEN);
g.drawRect(0, 0, Screen.getDimension().width, Screen.getDimension().height);
g.drawRect(1, 1, Screen.getDimension().width-1, Screen.getDimension().height-1);
g.drawRect(2, 2, Screen.getDimension().width-2, Screen.getDimension().height-2);
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).point.x, colorB.get(0).point.y, colorB.get(i).point.x, colorB.get(i).point.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<ColourPoint> colorB = new ArrayList<ColourPoint>();
private BufferedImage image = null;
private boolean consume = true;
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("Toggle Input");
private final JButton findB = new JButton("Find DTMS");
private final JPanel img = new JPanel();
private JTextField tolT = new JTextField("r: 10; g: 10; b: 10;");
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() {
y = 0;
pane.removeAll();
SP.removeAll();
sPane.removeAll();
pane.setPreferredSize(new Dimension(Screen.getDimension().width+20, Screen.getDimension().height + 340));
SP.setPreferredSize(new Dimension(Screen.getDimension().width, 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).point.x + ", " + colorB.get(i).point.y + ");");
colorT.get(i).setText("Color c" + i + " = new Color( " + colorB.get(i).colour.getRed() + ", " + colorB.get(i).colour.getGreen() + ", " + colorB.get(i).colour.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);
}
private Tolerance makeTolerance() {
String in = tolT.getText();
String [] rgb = new String [3];
int count = 0;
for(int i = 0; i < in.length(); i++){
char test = in.charAt(i);
if(test == ';'){
int j = i;
for(;j >= 0; j--){
if(in.charAt(j) == ':')break;
}
rgb[count] = in.substring(j+2, i);
count++;
}
}
return new Tolerance(Integer.parseInt(rgb[0]),Integer.parseInt(rgb[1]),Integer.parseInt(rgb[2]));
}
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;
}
});
findB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DTMPoint DTM_PT = new DTMPoint(colorB.get(0).colour, makeTolerance());
DTMSubPoint [] DTM_PTS = getPoints();
DTM dtm = new DTM(DTM_PT, DTM_PTS);
drawDTMS(DTMs.find(dtm, 0, 0, Screen.getDimension().width, Screen.getDimension().height),img.getGraphics());
}
private void drawDTMS(DTM[] DTMS, Graphics g) {
rects = new ArrayList<Rectangle>();
for(int i = 0; i < DTMS.length; i++){
rects.add(buildRect(DTMS[i]));
}
}
private Rectangle buildRect(DTM dtm) {
Point [] points = new Point [dtm.sub_points.length+1];
points [0] = dtm.main_point.point;
for(int i = 0; i < dtm.sub_points.length; i++){
points [i+1] = dtm.sub_points[i].colourPoint.point;
}
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 DTMSubPoint[] getPoints() {
try{
ArrayList<DTMSubPoint> points = new ArrayList<DTMSubPoint>();
for(int i = 1; i < colorB.size(); i++){
Point pt = new Point(colorB.get(i).point.x-colorB.get(0).point.x,colorB.get(i).point.y-colorB.get(0).point.y);
points.add(new DTMSubPoint(new ColourPoint(pt, colorB.get(i).colour), makeTolerance(), Integer.parseInt(radT.getText())));
}
return points.toArray(new DTMSubPoint[points.size()]);
}catch(IndexOutOfBoundsException ex){
return new DTMSubPoint[0];
}
}
});
copyB.addActionListener(new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
Tolerance tol = makeTolerance();
int dia = Integer.parseInt(radT.getText());
String mainPoint = "DTMPoint DTM_PT_"+ count+" = new DTMPoint(new Color(" + colorB.get(0).colour.getRed() + ", " + colorB.get(0).colour.getGreen() + ", " + colorB.get(0).colour.getBlue() + "), new Tolerance("+ tol.r + ", " + tol.g + ", " + tol.b+"))";
String subPoints = "DTMSubPoint [] DTM_PTS_"+ count+" = {";
for(int i = 1; i < pointT.size(); i++) {
subPoints += " new DTMSubPoint(new ColourPoint(new Point(" + (colorB.get(i).point.x-colorB.get(0).point.x) + ", " + (colorB.get(i).point.y-colorB.get(0).point.y) +"), new Color( " + colorB.get(i).colour.getRed() + ", " + colorB.get(i).colour.getGreen() + ", " + colorB.get(i).colour.getBlue() + ")), new Tolerance("+tol.r + ", " + tol.g + ", " + tol.b+"),"+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 = 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 ColourPoint(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).point;
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){}
}
});
}
}
@Override
public void run() {
if(onStart()){
while(!Thread.interrupted() && loop()>=0){}
onStop();
}else println("Script failed to start!");
}
private void onStop() {
}
public OVERRIDE_RETURN overrideKeyEvent(KeyEvent e) {
return OVERRIDE_RETURN.PROCESS;
}
public OVERRIDE_RETURN overrideMouseEvent(MouseEvent e) {
if(e.getID() == MouseEvent.MOUSE_CLICKED && consume){
e.consume();
loc = e.getPoint();
image = ClientContext.get().imagePanel.M;
//gui.img.getGraphics().drawImage(image, 0,0,image.getWidth(),image.getHeight(),null);
gameScache = to2D(image);
updateDrawBox();
return OVERRIDE_RETURN.DISMISS;
}else if(e.getID() == MouseEvent.MOUSE_PRESSED && consume)return OVERRIDE_RETURN.DISMISS;
else return OVERRIDE_RETURN.PROCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment