Skip to content

Instantly share code, notes, and snippets.

@TheBatScripts
Created June 1, 2012 20:39
Show Gist options
  • Save TheBatScripts/2855015 to your computer and use it in GitHub Desktop.
Save TheBatScripts/2855015 to your computer and use it in GitHub Desktop.
ExpUtil
package scripts;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import org.tribot.api.General;
import org.tribot.api.colour.GameTab;
import org.tribot.api.colour.types.Tolerance;
import org.tribot.api.input.Mouse;
import org.tribot.api.togl.Text;
import org.tribot.api.togl.types.TextChar;
import org.tribot.bot.ClientContext;
//@author TheBat,Aidden.
public class Skills {
/**
* Opens the stats tab.
*/
public static void openTab(){
GameTab.open(GameTab.TABS.STATS);
}
/**
* Returns whether the stats tab is open.
* @return boolean open - true if open.
* false if not open.
*/
public static boolean isOpen(){
return GameTab.getOpen().equals(GameTab.TABS.STATS);
}
/**
* 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;
}
/**
* Return the current amount of xp in the given skill.
* @param Skill skill - the skill that you want to read.
* @return int xp - The amount of xp.
*/
public static int getCurrentXP(SKILLS skill){
moveToSkill(skill);
General.sleep(100, 200);
Rectangle pop = getPopBounds();
int exp = 0;
TextChar [] chars = Text.findCharsInArea(pop.x+1, pop.y+16, pop.x+pop.width-1, 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 static void moveToSkill(SKILLS skill) {
Mouse.moveBox(skill.getBounds().x, skill.getBounds().y, skill.getBounds().x+skill.getBounds().width, skill.getBounds().y+skill.getBounds().height);
}
private static Rectangle getPopBounds(){
final Color edge = new Color(235,236,230);
Rectangle rec = SKILLS.BOUNDS;
Point [] points = getPoints(edge, rec, new Tolerance(0,0,0));
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 static Point [] getPoints(Color c, Rectangle rec, Tolerance tol){
BufferedImage image = ClientContext.get().imagePanel.j;
ArrayList<Point> builder = new ArrayList<Point>();
for(int y = rec.y; y < rec.y+rec.height; y++){
for(int x = rec.x; x < rec.x+rec.width; x++){
if(image.getRGB(x, y) == c.getRGB()) builder.add(new Point(x,y));
}
}
return builder.toArray(new Point[builder.size()]);
}
//Credit to Aidden for this
public enum SKILLS{
ATTACK("Attack", new Rectangle(550, 210, 59, 26)),
STRENGTH("Strength", new Rectangle(550, 238, 59, 26)),
DEFENCE("Defence", new Rectangle(550, 266, 59, 26)),
RANGED("Ranged", new Rectangle(550, 294, 59, 26)),
PRAYER("Prayer", new Rectangle(550, 322, 59, 26)),
MAGIC("Magic", new Rectangle(550, 350, 59, 26)),
RUNECRAFTING("Runecrafting", new Rectangle(550, 378, 59,26)),
CONSTRUCTION("Construction", new Rectangle(550, 406, 59, 26)),
DUNGEONEERING("Dungeoneering", new Rectangle(550, 434, 59, 26)),
CONSTITUTION("Constitution", new Rectangle(612, 210, 59, 26)),
AGILITY("Agility", new Rectangle(612, 238, 59, 26)),
HERBLORE("Herblore", new Rectangle(612, 266, 59, 26)),
THIEVING("Thieving", new Rectangle(612, 294, 59, 26)),
CRAFTING("Crafting", new Rectangle(612, 322, 59, 26)),
FLETCHING("Fletching", new Rectangle(612, 350, 59, 26)),
SLAYER("Slayer", new Rectangle(612, 378, 59, 26)),
HUNTER("Hunter", new Rectangle(612, 406, 59, 26)),
MINING("Mining", new Rectangle(674, 210, 59, 26)),
SMITHING("Smithing", new Rectangle(674, 238, 59, 26)),
FISHING("Fishing", new Rectangle(674, 266, 59, 26)),
COOKING("Cooking", new Rectangle(674, 294, 59, 26)),
FIREMAKING("Firemaking", new Rectangle(674, 322, 59, 26)),
WOODCUTTING("Woodcutting", new Rectangle(674, 350, 59, 26)),
FARMING("Farming", new Rectangle(674, 378, 59, 26)),
SUMMONING("Summoning", new Rectangle(674, 406, 59, 26));
private final String skill;
private final Rectangle bounds;
public static final Rectangle BOUNDS = new Rectangle(545, 206, 192, 260);
private SKILLS(final String skill, final Rectangle bounds) {
this.skill = skill;
this.bounds = bounds;
}
/**
* Gets the skill's name.
* @return The skill's name.
*/
public String getSkill() {
return skill;
}
/**
* Gets the skill's bounding rectangle.
* @return The skill's bounding rectangle.
*/
public Rectangle getBounds() {
return bounds;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment