Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created March 9, 2013 05:13
Show Gist options
  • Save TheBatScripts/5122672 to your computer and use it in GitHub Desktop.
Save TheBatScripts/5122672 to your computer and use it in GitHub Desktop.
package scripts;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import org.tribot.api.DTMs;
import org.tribot.api.General;
import org.tribot.api.Screen;
import org.tribot.api.input.Keyboard;
import org.tribot.api.input.Mouse;
import org.tribot.api.types.colour.ColourPoint;
import org.tribot.api.types.colour.DTM;
import org.tribot.api.types.colour.DTMPoint;
import org.tribot.api.types.colour.DTMSubPoint;
import org.tribot.api.types.colour.Tolerance;
import org.tribot.api2007.ChooseOption;
import org.tribot.api2007.Player;
import org.tribot.script.Script;
import org.tribot.script.ScriptManifest;
import org.tribot.script.interfaces.Ending;
import org.tribot.script.interfaces.Painting;
@ScriptManifest(authors = { "TheBat" }, category = "WoodCutting 07", name = "BatChopper", version = .1)
public class BatChopper extends Script implements Ending, Painting{
//DTMS
DTMPoint DTM_PT_tree = new DTMPoint(new Color(36, 44, 20), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_tree = {new DTMSubPoint(new ColourPoint(new Point(-7, 3), new Color(64, 74, 17)), new Tolerance(15, 15, 5), 3)};
DTM dtmTree = new DTM(DTM_PT_tree, DTM_PTS_tree);
DTMPoint DTM_PT_shafts = new DTMPoint(new Color(0, 0, 1), new Tolerance(5, 5, 5));
DTMSubPoint [] DTM_PTS_shafts = {new DTMSubPoint(new ColourPoint(new Point(26, -17), new Color(0, 0, 1)), new Tolerance(5, 5, 5), 1), new DTMSubPoint(new ColourPoint(new Point(12, 5), new Color(0, 0, 1)), new Tolerance(5, 5, 5), 1), new DTMSubPoint(new ColourPoint(new Point(19, -10), new Color(85, 51, 8)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(7, -11), new Color(85, 51, 8)), new Tolerance(5, 5, 5), 1)};
DTM dtmShafts = new DTM(DTM_PT_shafts, DTM_PTS_shafts);
DTMPoint DTM_PT_knife = new DTMPoint(new Color(109, 101, 101), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_knife = {new DTMSubPoint(new ColourPoint(new Point(-10, 17), new Color(85, 57, 8)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(-15, 26), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(1, -3), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(2, 0), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1)};
DTM dtmKnife = new DTM(DTM_PT_knife, DTM_PTS_knife);
DTMPoint DTM_PT_hatchet = new DTMPoint(new Color(108, 80, 13), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_hatchet = {new DTMSubPoint(new ColourPoint(new Point(-7, 25), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(3, 0), new Color(151, 113, 17)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(0, 12), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1)};
DTM dtmHatchet = new DTM(DTM_PT_hatchet, DTM_PTS_hatchet);
DTMPoint DTM_PT_ChatSword = new DTMPoint(new Color(63, 159, 156), new Tolerance(30, 30, 30));
DTMSubPoint [] DTM_PTS_ChatSword = {new DTMSubPoint(new ColourPoint(new Point(-2, -6), new Color(85, 75, 16)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(-2, 6), new Color(116, 106, 34)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(-13, 0), new Color(220, 213, 153)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(-15, 0), new Color(1, 49, 43)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(41, 0), new Color(106, 107, 109)), new Tolerance(20, 20, 20), 1)};
DTM dtmChatSword = new DTM(DTM_PT_ChatSword, DTM_PTS_ChatSword);
DTMPoint DTM_PT_ChatShafts = new DTMPoint(new Color(108, 65, 13), new Tolerance(30, 30, 30));
DTMSubPoint [] DTM_PTS_ChatShafts = {new DTMSubPoint(new ColourPoint(new Point(19, -32), new Color(85, 51, 8)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(20, 7), new Color(108, 65, 13)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(43, -30), new Color(85, 51, 8)), new Tolerance(20, 20, 20), 1), new DTMSubPoint(new ColourPoint(new Point(34, -35), new Color(85, 51, 8)), new Tolerance(20, 20, 20), 1)};
DTM dtmChatShafts = new DTM(DTM_PT_ChatShafts, DTM_PTS_ChatShafts);
DTMPoint DTM_PT_EnterAmount = new DTMPoint(new Color(0, 0, 0), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_EnterAmount = {new DTMSubPoint(new ColourPoint(new Point(82, 1), new Color(0, 0, 0)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(43, 30), new Color(0, 0, 128)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(44, 30), new Color(0, 0, 128)), new Tolerance(10, 10, 10), 1)};
DTM dtmEnterAmount = new DTM(DTM_PT_EnterAmount, DTM_PTS_EnterAmount);
DTMPoint DTM_PT_InvLogs = new DTMPoint(new Color(175, 142, 80), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_InvLogs = {new DTMSubPoint(new ColourPoint(new Point(-6, 7), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(18, -8), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(24, 8), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(4, 16), new Color(0, 0, 1)), new Tolerance(10, 10, 10), 1)};
DTM dtmInvLog = new DTM(DTM_PT_InvLogs, DTM_PTS_InvLogs);
DTMPoint DTM_PT_LevelArrows = new DTMPoint(new Color(96, 89, 89), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_LevelArrows = {new DTMSubPoint(new ColourPoint(new Point(76, -65), new Color(108, 20, 13)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(28, 13), new Color(154, 31, 17)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(76, -37), new Color(92, 86, 86)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(84, -61), new Color(138, 28, 17)), new Tolerance(10, 10, 10), 1)};
DTM dtmLevelArrows = new DTM(DTM_PT_LevelArrows, DTM_PTS_LevelArrows);
DTMPoint DTM_PT_ClickHere = new DTMPoint(new Color(0, 0, 255), new Tolerance(10, 10, 10));
DTMSubPoint [] DTM_PTS_ClickHere = {new DTMSubPoint(new ColourPoint(new Point(-4, 6), new Color(0, 0, 255)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(0, 10), new Color(0, 0, 255)), new Tolerance(10, 10, 10), 1), new DTMSubPoint(new ColourPoint(new Point(58, 10), new Color(0, 0, 255)), new Tolerance(10, 10, 10), 1)};
DTM dtmClickHere = new DTM(DTM_PT_ClickHere, DTM_PTS_ClickHere);
//Script logic variables
int timeOut;
Boolean enterAmount = false;
public boolean onStart(){
return true;
}
private int loop(){
DTM[] list;
//sleep if we are doing an action or moving
if(isMoving() || Player.getAnimation() == 879 || Player.getAnimation() == 1248)sleep(100);
//if we aren't moving, check if we are trying make shafts
else if(ChooseOption.isOptionValid("Make X"))ChooseOption.select("Make X");
else if(DTMs.find(dtmLevelArrows, 6, 344, 513, 474).length > 0){
Rectangle rect = buildRect(DTMs.find(dtmClickHere, 6, 344, 513, 474)[0]);
Mouse.click(General.random(rect.x, rect.x+rect.width), General.random(rect.y, rect.y+rect.height), 1);
}
else if(DTMs.find(dtmChatSword, 6, 344, 513, 474).length > 0){
Rectangle rect = buildRect(DTMs.find(dtmChatShafts, 6, 344, 513, 474)[0]);
Mouse.click(General.random(rect.x, rect.x+rect.width), General.random(rect.y, rect.y+rect.height), 3);
sleep(150,300);
//check if we need to type an amount
}else if(DTMs.find(dtmEnterAmount, 6, 344, 513, 474).length > 0){
Keyboard.typeSend("27");
enterAmount = false;
sleep(600,800);
}
//check if we need to enter an amount but the screen wasn't up yet
else if(enterAmount)sleep(100);
//check if the inventory is full
else if(inventoryFull()){
list = DTMs.find(dtmKnife, 550, 204, 733, 465);
if(list.length > 0){
Rectangle rect = buildRect(list[0]);
Mouse.click(General.random(rect.x, rect.x+rect.width), General.random(rect.y, rect.y+rect.height), 1);
sleep(100,150);
list = DTMs.find(dtmInvLog, 550, 204, 733, 465);
if(list.length > 0){
rect = buildRect(list[0]);
Mouse.click(General.random(rect.x, rect.x+rect.width), General.random(rect.y, rect.y+rect.height), 1);
}
}
}else if((list = DTMs.find(dtmTree, 4, 4, 516, 338)).length > 0){
Rectangle rect = getClosest(DTMSToRects(list), new Point(260,173));
Mouse.click(General.random(rect.x, rect.x+rect.width), General.random(rect.y, rect.y+rect.height), 1);
sleep(150,300);
}else return -1;
return 100;
}
public void onEnd() {
}
public void onPaint(Graphics g) {
}
public Rectangle getClosest(Rectangle[] rects, Point pt){
Rectangle best = rects[0];
for(int i = 1; i < rects.length; i++){
if(Math.abs(best.getCenterX()-pt.x) >= Math.abs(rects[i].getCenterX()-pt.x)){
if(Math.abs(best.getCenterY()-pt.y) >= Math.abs(rects[i].getCenterY()-pt.y)){
best = rects[i];
}
}
}
return best;
}
private boolean isMoving(){
int x = Player.getPosition().getX();
int y = Player.getPosition().getX();
sleep(800);
if(x == Player.getPosition().getX() && y == Player.getPosition().getX())return false;
return true;
}
private boolean inventoryFull() {
BufferedImage image = General.isEOCClient()?Screen.captureDebugImage():org.tribot.api2007.Screen.getGameImage();
for(Slot slot:Slot.values()){
int totalr = 0;
int totalb = 0;
int totalg = 0;
int total = 0;
for(int y = slot.bounds.y; y <slot.bounds.y+slot.bounds.height; y++){
for(int x = slot.bounds.x; x < slot.bounds.x+slot.bounds.width; x++){
totalr += new Color(image.getRGB(x, y)).getRed();
totalg += new Color(image.getRGB(x, y)).getGreen();
totalb += new Color(image.getRGB(x, y)).getBlue();
total++;
}
}
if(Math.abs((totalr/total) - slot.color.getRed()) < 1){
if(Math.abs((totalg/total) - slot.color.getGreen()) < 1){
if(Math.abs((totalb/total) - slot.color.getBlue()) < 1){
println(slot + " " + new Color((totalr/total), (totalg/total), (totalb/total)) + " " + slot.color);
return false;
}
}
}
}
return true;
}
@Override
public void run() {
if(onStart()){
while(!Thread.interrupted() && loop()>=0){}
}else println("Script failed to start!");
}
public Rectangle[] DTMSToRects(DTM[] DTMS) {
Rectangle rects[] = new Rectangle[DTMS.length];
for(int i = 0; i < DTMS.length; i++){
rects[i] = buildRect(DTMS[i]);
}
return rects;
}
public 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);
}
public static enum Slot {
SLOT_0(new Rectangle(561, 212, 36, 32), new Color(71,62,53)),
SLOT_1(new Rectangle(603, 212, 36, 32), new Color(73,63,54)),
SLOT_2(new Rectangle(645, 212, 36, 32), new Color(71,61,52)),
SLOT_3(new Rectangle(687, 212, 36, 32), new Color(72,63,53)),
SLOT_4(new Rectangle(561, 248, 36, 32), new Color(71,62,53)),
SLOT_5(new Rectangle(603, 248, 36, 32), new Color(71,62,52)),
SLOT_6(new Rectangle(645, 248, 36, 32), new Color(71,61,52)),
SLOT_7(new Rectangle(687, 248, 36, 32), new Color(72,63,54)),
SLOT_8(new Rectangle(561, 284, 36, 32), new Color(70,61,52)),
SLOT_9(new Rectangle(603, 284, 36, 32), new Color(72,63,54)),
SLOT_10(new Rectangle(645, 284, 36, 32), new Color(71,62,53)),
SLOT_11(new Rectangle(687, 284, 36, 32), new Color(71,62,53)),
SLOT_12(new Rectangle(561, 320, 36, 32), new Color(72,63,54)),
SLOT_13(new Rectangle(603, 320, 36, 32), new Color(73,64,55)),
SLOT_14(new Rectangle(645, 320, 36, 32), new Color(74,65,56)),
SLOT_15(new Rectangle(687, 320, 36, 32), new Color(74,65,56)),
SLOT_16(new Rectangle(561, 356, 36, 32), new Color(72,63,54)),
SLOT_17(new Rectangle(603, 356, 36, 32), new Color(72,63,55)),
SLOT_18(new Rectangle(645, 356, 36, 32), new Color(73,64,56)),
SLOT_19(new Rectangle(687, 356, 36, 32), new Color(76,67,59)),
SLOT_20(new Rectangle(561, 392, 36, 32), new Color(70,60,51)),
SLOT_21(new Rectangle(603, 392, 36, 32), new Color(70,61,52)),
SLOT_22(new Rectangle(645, 392, 36, 32), new Color(71,62,53)),
SLOT_23(new Rectangle(687, 392, 36, 32), new Color(74,65,57)),
SLOT_24(new Rectangle(561, 428, 36, 32), new Color(69,60,50)),
SLOT_25(new Rectangle(603, 428, 36, 32), new Color(69,60,51)),
SLOT_26(new Rectangle(645, 428, 36, 32), new Color(71,62,53)),
SLOT_27(new Rectangle(687, 428, 36, 32), new Color(73,64,55));
public final Rectangle bounds;
public final Color color;
private Slot(final Rectangle bounds, final Color col) {
this.bounds = bounds;
color = col;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment