Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created June 4, 2012 04:29
Show Gist options
  • Save TheBatScripts/2866350 to your computer and use it in GitHub Desktop.
Save TheBatScripts/2866350 to your computer and use it in GitHub Desktop.
BatIvy 1.2
package scripts;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.tribot.api.General;
import org.tribot.api.Timing;
import org.tribot.api.colour.Screen;
import org.tribot.api.colour.types.Tolerance;
import org.tribot.api.input.Keyboard;
import org.tribot.api.input.Mouse;
import org.tribot.api.togl.Game;
import org.tribot.api.togl.ScreenModels;
import org.tribot.api.togl.Text;
import org.tribot.api.togl.types.ScreenModel;
import org.tribot.api.togl.types.TextChar;
import org.tribot.bot.ClientContext;
import org.tribot.script.Script;
import org.tribot.script.ScriptManifest;
import org.tribot.script.interfaces.Painting;
@ScriptManifest (authors = { "TheBat" }, category = "WoodCutting", name = "BatIvy",
description="<html>"+
"<h1 align=\"center\"><img style=\"WIDTH: 393px; HEIGHT: 229px\" "+
"border=0 hspace=0 src=\"http://i.imgur.com/FPDbJ.png\" width=393 height=229></h1>"+
"</html>")
public class BatIvy extends Script implements Painting {
Point[] IVY_DTM_PTS = { new Point( 279, 71), new Point( 278, 65)};
Color[] IVY_DTM_RGB = { new Color( 93, 124, 64), new Color( 100, 134, 68)};
DTM dtmIvy = new DTM(IVY_DTM_PTS, IVY_DTM_RGB);
Point[] BIG_IVY_DTM_PTS = { new Point( 250, 128), new Point( 254, 105), new Point( 265, 116), new Point( 264, 129)};
Color[] BIG_IVY_DTM_RGB = { new Color( 95, 127, 66), new Color( 99, 132, 68), new Color( 94, 125, 65), new Color( 95, 127, 66)};
DTM dtmBigIvy = new DTM(BIG_IVY_DTM_PTS, BIG_IVY_DTM_RGB);
final Rectangle VIEWPORT = new Rectangle(3, 3, 513, 334);
long TIME = 0;
int centerX = 0;
int centerY = 0;
String status = "Finding ivy";
ScreenModel player = null;
int tempL = -1;
private boolean showPaint = true;
Rectangle hideR = new Rectangle(450,300,50,20);
private long CUR_ID = 0;
private int cut = 0;
private double version = 1.2;
private int templvl = 300;
private int levelsGained = -2;
private int startEXP = -1;
private double EXP = 332.5;
int oAm = 0;
ExpChecker xpCheck = new ExpChecker();
private long lastAnti = 0;
private boolean first = true;
private int trys = 0;
private long SSTIME;
private boolean onStart() {
centerX = (int) VIEWPORT.getCenterX();
centerY = (int) VIEWPORT.getCenterY();
while(startEXP < 0){
try{
startEXP = getEXP();
}catch(Exception e){
startEXP = -1;
}
}
Mouse.click(new Point(659,187), 1);
templvl = convertToLevel(startEXP);
if(!dtmBigIvy.findDTMS(21,VIEWPORT))rotateToIvy();
player = getPlayer();
lastAnti = System.currentTimeMillis();
SSTIME = System.currentTimeMillis();
TIME = System.currentTimeMillis();
levelsGained = 0;
return true;
}
private int getEXP() {
Mouse.click(new Point(598,187), 1);
int exp = 0;
Mouse.move(new Point(704,361));
sleep(100);
TextChar [] chars = Text.findCharsInArea(609, 415, 130, 15, true);
String xpS = Text.lineToString(chars, 0);
int i = xpS.length()-1;
for(; i >= 0; i--){
if(xpS.charAt(i) == ':')break;
}
xpS = xpS.substring(i+1).replaceAll(",", "");
exp = Integer.parseInt(xpS);
return exp;
}
private int loop() {
if(first ){
xpCheck.start();
first = false;
}
if((System.currentTimeMillis()-SSTIME) > 3600000){
SSTIME = System.currentTimeMillis();
BufferedImage image = ClientContext.get().imagePanel.j;
onPaint(image.getGraphics());
try {
File file = new File("./Storage/ScreenShots/ScreenShot.png");
file.getParentFile().mkdirs();
ImageIO.write(image, "png",file);
} catch (IOException ex) {
}
}
Mouse.setSpeed(General.random(130, 150));
if(System.currentTimeMillis()-lastAnti>240000){
Mouse.moveBox(VIEWPORT.x, VIEWPORT.y, VIEWPORT.x+VIEWPORT.width, VIEWPORT.y+VIEWPORT.height);
Keyboard.pressKey((General.random(1,2) > 1) ? (char)KeyEvent.VK_LEFT : (char)KeyEvent.VK_RIGHT);
sleep(General.random(100, 200));
Keyboard.releaseKey((char)KeyEvent.VK_LEFT);
Keyboard.releaseKey((char)KeyEvent.VK_RIGHT);
lastAnti = System.currentTimeMillis();
}
if(status.equals("Finding ivy")){
if(trys >= 6){
rotateToIvy();
trys = 0;
}
if(dtmIvy.findDTMS(20, VIEWPORT)){
Rectangle rec = dtmIvy.getList().get(General.random(0,dtmIvy.getList().size()-1));
Mouse.move(new Point(General.random(rec.x, rec.x + rec.width),General.random(rec.y, rec.y + rec.height)));
sleep(General.random(100, 200));
try{
if(Game.getUptext().contains("Chop")){
Mouse.click(Mouse.getPos(), 1);
trys = 0;
}else{
trys++;
return 100;
}
}catch(Exception e){
return 100;
}
int k = 0;
while(k <= 20 && getPlayer().id == player.id){
sleep(100);
}
if(k<20)status = "Chopping";
}else rotateToIvy();
return 100;
}
if(status.equals("Chopping")){
try{
int k = 0;
while(k <= 8 && getPlayer().id == player.id){
sleep(100);
k++;
}
if(k > 8)status = "Finding ivy";
else{
if(General.random(0,180000000) < 100){
Mouse.moveBox(VIEWPORT.x, VIEWPORT.y, VIEWPORT.x+VIEWPORT.width, VIEWPORT.y+VIEWPORT.height);
Keyboard.pressKey((General.random(1,2) > 1) ? (char)KeyEvent.VK_LEFT : (char)KeyEvent.VK_RIGHT);
sleep(General.random(100, 200));
Keyboard.releaseKey((char)KeyEvent.VK_LEFT);
Keyboard.releaseKey((char)KeyEvent.VK_RIGHT);
lastAnti = System.currentTimeMillis();
}
}
}catch(NullPointerException e){}
return 100;
}
return 100;
}
private void rotateToIvy() {
Mouse.clickBox(535, 15,552, 36, 1);
Keyboard.pressKey((char)KeyEvent.VK_UP);
sleep(600);
Keyboard.releaseKey((char)KeyEvent.VK_UP);
Keyboard.pressKey((General.random(1,2) > 1) ? (char)KeyEvent.VK_LEFT : (char)KeyEvent.VK_RIGHT);
while(!dtmBigIvy.findDTMS(21,VIEWPORT)){
sleep(50);
dtmIvy.getList().clear();
}
Keyboard.releaseKey((char)KeyEvent.VK_LEFT);
Keyboard.releaseKey((char)KeyEvent.VK_RIGHT);
}
private ScreenModel getPlayer() {
int k = -1;
ScreenModel ply = null;
while(k < 0){
try{
ScreenModel [] allMods = ScreenModels.getAll();
if(allMods.length > 0 && allMods[0] != null)ply = allMods[0];
else continue;
for(int i = 0; i < allMods.length; i++){
ScreenModel test = allMods[i];
if(test != null && test.id != 3397519456L){
if(Point.distance(test.base_point.x, test.base_point.y, centerX, centerY)<
Point.distance(ply.base_point.x, ply.base_point.y, centerX, centerY)){
if(VIEWPORT.contains(test.base_point))ply = test;
}
}
}
k = 3;
}catch(NullPointerException e){
k = -1;
}
}
CUR_ID = ply.id;
return ply;
}
@SuppressWarnings("deprecation")
private void onStop() {
xpCheck.interrupt();
xpCheck.destroy();
}
public void onPaint(Graphics g) {
final Color OPAQUE_BLACK = new Color(0, 0, 0, 180);
final Color GREEN = new Color(Color.GREEN.getRed(), Color.GREEN.getGreen(), Color.GREEN.getBlue(), 175).darker();
final int CUR_XP = (int) (startEXP + (EXP * cut));
final int CUR_LVL = convertToLevel(CUR_XP);
if(templvl < CUR_LVL) levelsGained++;
templvl = CUR_LVL;
final double percentage = percentToNext(CUR_XP);
if(showPaint){
g.setColor(new Color(0,0,0,175));
g.fill3DRect(1, 215, 515, 123, true);
int idx = 213;
g.setColor(Color.WHITE);
g.drawString("Time Running: " + Timing.msToString(System.currentTimeMillis() - TIME), 5, idx+=20);
g.setColor(Color.GREEN);
g.drawString("BatIvy", 270,idx);
g.setColor(Color.WHITE);
g.drawString("Bat", 270,idx);
g.drawString("Total cut: " + cut, 5, idx+=20);
g.drawString("Version " + version, 270,idx);
try{
g.drawString("Player ID: " + player.id + "/" + CUR_ID, 5, idx+=20);
}catch(Exception e){
g.drawString("Player ID: Finding", 5, idx+=20);
}
try{
g.drawString("Exp./Hour: " + (int)(EXP *(3600000*(long)cut)/(System.currentTimeMillis() - TIME)), 5, idx+=20);
}catch(Exception e){}
g.drawString("Status: " + status, 5, idx+=20);
g.setColor(OPAQUE_BLACK);
g.fill3DRect(5, idx+=5, 270, 17, true);
g.setColor(GREEN);
g.fillRect(6, idx+1, (int) (268 * percentage), 15);
g.setColor(Color.WHITE);
try{
g.drawString("Current Level: " + CUR_LVL + "(+" + levelsGained + ") | " + (int)(100*percentage) +"% to " + (CUR_LVL+1) + " | TTL: " + Timing.msToString((System.currentTimeMillis() - TIME)*(xpRemaing(CUR_XP))/(long)(EXP * cut)),8,idx+=13);
}catch(Exception e){
g.drawString("Current Level: " + CUR_LVL + "(+" + levelsGained + ") | " + (int)(100*percentage) +"% to " + (CUR_LVL+1) + " | TTL: " + Timing.msToString(0),8,idx+=13);
}
}else{
g.setColor(Color.BLACK);
g.drawRect(hideR.x, hideR.y, hideR.width, hideR.height);
g.drawString("Show",hideR.x+10,hideR.y+15);
}
g.setColor(Color.RED);
g.fillOval(centerX-2, centerY-2, 4, 4);
g.drawRect(VIEWPORT.x, VIEWPORT.y, VIEWPORT.width, VIEWPORT.height);
}
/**
* Gives the percent to the next level given the current exp amount. (multiply by 100 for common percent value).
* @param int xp - the current amount of xp.
* @return double percent - the percent of the next level complete.
*/
public static double percentToNext(int xp){
return 1-(double)xpRemaing(xp)/(double)(minXPForLevel(convertToLevel(xp))-minXPForLevel(convertToLevel(xp)-1));
}
/**
* Gives the total amount of experience needed to attain the next level up.
* @param int xp - the current amount of xp.
* @return int xp - the amount remaining.
*/
public static int xpRemaing(int xp) {
return minXPForLevel(convertToLevel(xp))-xp;
}
/**
* Gives the minimum amount of experience needed to attain the given level.
* @param int level - the level that needs to be reached.
* @return the minimum amount of experience possible at that level.
*/
public static int minXPForLevel(int level) {
if(level > 0)return (int)(.25*(int)(level + 300.0 * Math.pow(2.0, (double)level/7.0)) + (int)minXPForLevel(--level));
return 0;
}
/**
* Returns the maximum level possible at the given xp amount.
* @param xp - the current amount of xp.
* @return int level - the maximum level possible at the given xp amount.
*/
public static int convertToLevel(int xp) {
for(int i = 1; i < 121; i++){
if(xp < minXPForLevel(i)){
return i;
}
}
return 1;
}
public void run() {
if(onStart()){
while(!Thread.interrupted() && loop()>=0){}
onStop();
}else println("Script failed to start!");
}
class DTM {
private final Color[] rgb;
private final Point[] loc;
private Rectangle box;
private final String name;
public ArrayList<Rectangle> dtmLocations = new ArrayList<Rectangle>();
private ArrayList<Rectangle> dtmLocationsNS = new ArrayList<Rectangle>();
public DTM(final Point[] loc, final Color[] color, final String name) {
this.loc = loc;
this.rgb = color;
this.name = name;
box = createRectangleFromPoints(loc);
}
public DTM(final Point[] loc, final Color[] color) {
this(loc, color, "DTM");
}
/**
* Given a set of points create a rectangle that encases all of those points
* @param points : set of points to create a rectangle with
* @return : A rectangle that encloses all points
*/
private Rectangle createRectangleFromPoints(final Point[] points) {
Point minPoint = new Point(800, 600), maxPoint = new Point(0, 0);
for(int i = 0; i < loc.length; i++) {
if(points[i].x < minPoint.x) minPoint.x = points[i].x;
if(points[i].x > maxPoint.x) maxPoint.x = points[i].x;
if(points[i].y < minPoint.y) minPoint.y = points[i].y;
if(points[i].y > maxPoint.y) maxPoint.y = points[i].y;
}
return new Rectangle(minPoint.x, minPoint.y, (maxPoint.x - minPoint.x) + 1, (maxPoint.y - minPoint.y) + 1);
}
/**
* Looks for the DTM in the original spot specified
* @param tol : tolerance between each RGB value when checking if colors are the same
* @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
*/
public boolean findOriginalDTM(final int tol) {
return checkAllPoints(loc[0], tol);
}
/**
* Finds all DTMs on the game screen stores and stores there location to the dtmLocations ArrayList.
* @param tol : tolerance between each RGB value when checking if colors are the same
* @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
*/
public boolean findDTMS(final int tol) {
return findDTMS(tol, new Rectangle(0, 0, Screen.getDimension().width, Screen.getDimension().height));
}
/**
* Searches for DTMs in in the rectangle passed and stores all DTMs found to the dtmLocations ArrayList.
* @param tol : tolerance between each RGB value when checking if colors are the same
* @param rec : Rectangle to search for the DTM to be in.
* @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
*/
public boolean findDTMS(final int tol, final Rectangle rec) {
boolean foundDTM = false;
dtmLocations.clear();
dtmLocationsNS.clear();
for(int y = rec.y; y < rec.y + rec.height; y++) {
for(int x = rec.x; x < rec.x + rec.width; x++) {
if(checkColor(Screen.getColourAt(new Point(x, y)), rgb[0], tol)) {
final Rectangle curbox = new Rectangle(x - (loc[0].x - box.x), y - (loc[0].y - box.y), box.width, box.height);
if(checkAllPoints(new Point(x, y), tol)) {
dtmLocations.add(curbox);
dtmLocationsNS.add(curbox);
foundDTM = true;
}
}
}
}
return foundDTM;
}
/**
* Grabs the DTM specified by index in the dtms found array
* @param i : index in the dtm array to grab
* @return : the center point of the rectangle for the DTM or null if not found
*/
public Point getDTM(final int i) {
return dtmLocationsNS.isEmpty() ? null : new Point(dtmLocationsNS.get(i).x + (dtmLocationsNS.get(i).width / 2), dtmLocationsNS.get(i).y + (dtmLocationsNS.get(i).height / 2));
}
/**
*
* @param gameS game screen colors array
* @param locM location of point that matches the first color in the array list colorB
* @return true if all colors match
*/
private boolean checkAllPoints(Point locM, final int tol) {
try {
final Point locI = loc[0];
for(int i = 0; i < loc.length; i++) {
// we can come back and shorten this, but for now this is to make sure we make no mistakes
final Color initialColor = rgb[i];
final Point initialColorPoint = loc[i];
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;
}
}
/**
* Checks to see if the 2 colors passed match
* @param c : Color 1
* @param c2 : Color 2
* @param tol : tolerance between each RGB value when checking if colors are the same
* @return <tt>true</tt> if colors match; otherwise <tt>false</tt>
*/
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));
}
/**
* Checks to see if the 2 RGB values match
* @param RGB1 : RGB value 1
* @param RGB2 : RGB value 2
* @param tol : tolerance between the RGB values when checking if they are the same
* @return <tt>true</tt> if RGB values match; otherwise <tt>false</tt>
*/
private boolean checkColor(final int RGB1, final int RGB2, final int Tol) {
return Math.abs(RGB1 - RGB2) < Tol;
}
public void drawDTMs(Graphics g) {
g.setColor(Color.RED);
for(int i = 0; i < dtmLocationsNS.size(); i++) {
drawX(g, dtmLocationsNS.get(i));
}
}
private void drawX(Graphics g, Rectangle rec) {
g.setColor(Color.RED);
g.drawLine(rec.x, rec.y, rec.x + rec.width, rec.y + rec.height);
g.drawLine(rec.x, rec.y + rec.height, rec.x + rec.width, rec.y);
}
public void drawDTMSquares(Graphics g, final Color c1, boolean addname) throws Exception {
for(int i = 0; i < dtmLocationsNS.size(); i++) {
Rectangle dtm = dtmLocationsNS.get(i);
g.setColor(c1);
g.drawRect(dtm.x, dtm.y, dtm.width, dtm.height);
if(!addname) return;
g.setColor(Color.BLACK);
g.fill3DRect(dtm.x + dtm.width - 33, dtm.y + dtm.height + 4, 41, 11, true);
g.setColor(Color.WHITE);
g.drawString(name + " " + i, dtm.x + dtm.width - 30, dtm.y + dtm.height + 13);
}
}
public void fancydraw(Graphics g, Color recColor, boolean addname, boolean addX) {
try {
if(dtmLocationsNS.isEmpty()) return;
g.setFont(g.getFont().deriveFont((float) 9));
for(int i = 0; i < dtmLocationsNS.size(); i++) {
drawDTMSquares(g, recColor, addname);
}
if(addX) drawX(g, dtmLocationsNS.get(0));
} catch (Exception e) {
General.println("Exception occured in paint");
}
}
public ArrayList<Rectangle> getList() {
return dtmLocationsNS;
}
}
class ExpChecker extends Thread implements Runnable {
private boolean coolDown = false;
long lastDetect;
public ExpChecker() {
this.setPriority(MIN_PRIORITY);
lastDetect = System.currentTimeMillis();
}
@Override
public void run() {
try{
while (!Thread.interrupted()) {
if(!coolDown)sleep(10);
if(!coolDown && Screen.coloursMatch(Screen.getColourAt(new Point(253,166)), new Color(245,178,65),new Tolerance(10,10,10))){
coolDown = true;
lastDetect = System.currentTimeMillis();
cut++;
}
if(coolDown && (System.currentTimeMillis()-lastDetect) > 600){
coolDown = false;
}
}
} catch (InterruptedException e) {
} finally{}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment