Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created June 4, 2012 03:33
Show Gist options
  • Save TheBatScripts/2866170 to your computer and use it in GitHub Desktop.
Save TheBatScripts/2866170 to your computer and use it in GitHub Desktop.
DTMBuilder TriBot
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.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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import org.tribot.api.colour.Screen;
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 static boolean flash = false;
private boolean OPEN = true;
public boolean onStart() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI();
}
});
return true;
}
public int loop() {
flash = !flash;
if(!OPEN)return -1;
return 1000;
}
private ArrayList<Rectangle> DTMloc = 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).loc.x < minPoint.x) minPoint.x = colorB.get(i).loc.x;
if(colorB.get(i).loc.x > maxPoint.x) maxPoint.x = colorB.get(i).loc.x;
if(colorB.get(i).loc.y < minPoint.y) minPoint.y = colorB.get(i).loc.y;
if(colorB.get(i).loc.y > maxPoint.y) maxPoint.y = colorB.get(i).loc.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).rgb, tol)) {
final Rectangle curbox = new Rectangle(x - (colorB.get(0).loc.x - box.x), y - (colorB.get(0).loc.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).loc;
for(int i = 0; i < colorB.size(); i++) {
final Color initialColor = colorB.get(i).rgb;
final Point initialColorPoint = colorB.get(i).loc;
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;
}
@Override
public void onPaint(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);
if(flash) {
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).loc.x, colorB.get(0).loc.y, colorB.get(i).loc.x, colorB.get(i).loc.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<ColorBox> colorB = new ArrayList<ColorBox>();
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 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 findB = new JButton("Find DTMS");
private JTextField tolT = new JTextField("20");
private final Font normalFont = new Font("Book antiqua", Font.PLAIN, 16);
// GUI COMPONENTS
GridBagConstraints c;
Container pane;
// 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(Component comp, int gridx, int gridy, int gridwidth,
double weightx) {
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.weightx = weightx;
pane.add(comp, c);
}
// easy placement of panels
void getSelected() {
for(int i = 0; i < pointT.size(); i++) {
}
}
void makeGUI() {
pane.removeAll();
pane.setPreferredSize(new Dimension(400, 160 + (selectB.size() * 70)));
addToGrid(headerL, 0, 0, 3, 1.0);
addToGrid(addB, 0, 1, 1, .5);
addToGrid(clearB, 1, 1, 2, .5);
addToGrid(findB, 0, 2, 1, 0.5);
addToGrid(tolL, 1, 2, 1, 0.40);
addToGrid(tolT, 2, 2, 1, 0.60);
addToGrid(copyB, 0, 3, 3, 1.0);
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).loc.x + ", " + colorB.get(i).loc.y + ");");
colorT.get(i).setText("Color c" + i + " = new Color( " + colorB.get(i).rgb.getRed() + ", " + colorB.get(i).rgb.getGreen() + ", " + colorB.get(i).rgb.getBlue() + ");");
addToGrid(selectB.get(i), 0, (4+ (i*2)), 1, 0.5);
addToGrid(pointT.get(i), 1, (4+ (i*2)), 2, 0.5);
addToGrid(removeB.get(i), 0, (5+ (i*2)), 1, 0.5);
addToGrid(colorT.get(i), 1, (5+ (i*2)), 2, 0.5);
}
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);
setLocationRelativeTo(getOwner());
makeGUI();
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();
makeGUI();
}
});
findB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
findDTMS(Integer.parseInt(tolT.getText()));
}
});
copyB.addActionListener(new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
int tol = Integer.parseInt(tolT.getText());
String mainPoint = "DTMPoint DTM_PT_"+ count+" = new DTMPoint(new Color(" + colorB.get(0).rgb.getRed() + ", " + colorB.get(0).rgb.getGreen() + ", " + colorB.get(0).rgb.getBlue() + "), new Tolerance("+ tol + ", " + tol + ", " + tol+"))";
String subPoints = "DTMSubPoint [] DTM_PTS_"+ count+" = {";
for(int i = 1; i < pointT.size(); i++) {
subPoints += " new DTMSubPoint(new ColourPoint(new Point(" + (colorB.get(i).loc.x-colorB.get(0).loc.x) + ", " + (colorB.get(i).loc.y-colorB.get(0).loc.y) +"), new Color( " + colorB.get(i).rgb.getRed() + ", " + colorB.get(i).rgb.getGreen() + ", " + colorB.get(i).rgb.getBlue() + ")), new Tolerance("+ tol + ", " + tol + ", " + tol+"),1),";
}
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);
}
});
addB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
colorB.add(curBox);
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).loc;
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();
}
});
}
}
@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){
e.consume();
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 {
BufferedImage image = ClientContext.get().imagePanel.j;
gameScache = to2D(image);
}
updateDrawBox();
}
return OVERRIDE_RETURN.DISMISS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment