Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.awt.Polygon;
- import java.awt.Rectangle;
- import java.awt.geom.Ellipse2D;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import org.rsbot.script.methods.Calculations;
- import org.rsbot.script.methods.Menu;
- import org.rsbot.script.methods.Mouse;
- import org.rsbot.script.methods.Tiles;
- import org.rsbot.script.wrappers.RSCharacter;
- import org.rsbot.script.wrappers.RSGroundItem;
- import org.rsbot.script.wrappers.RSModel;
- import org.rsbot.script.wrappers.RSObject;
- /**
- * A class for clicking on objects via their model. Uses normally distributed
- * points inside bounding ellipses
- *
- * @author Equinox_
- *
- */
- public class ModelMouse {
- // Sin & Cos Tables
- static Map<Integer, Double> sinTable;
- static Map<Integer, Double> cosTable;
- static {
- cacheTrig();
- }
- public static void cacheTrig() {
- sinTable = new HashMap<Integer, Double>();
- cosTable = new HashMap<Integer, Double>();
- }
- public static void unCacheTrig() {
- sinTable = null;
- cosTable = null;
- }
- public static double randomLinearDistribution(double min, double max) {
- return Math.min(min, max) + new Random().nextDouble()
- * Math.abs(max - min);
- }
- public static double randomNormalDistribution(double MIN, double MAX) {
- double mean = MIN + (MAX - MIN) / 2d;
- double rand;
- double sd = (MAX - MIN) / 3d;
- do {
- rand = (new Random().nextGaussian() * sd + mean);
- } while (rand < MIN || rand >= MAX);
- return rand;
- }
- public static Rectangle getBoundingBox(Point[] points) {
- int min_x = Integer.MAX_VALUE;
- int min_y = Integer.MAX_VALUE;
- int max_x = Integer.MIN_VALUE;
- int max_y = Integer.MIN_VALUE;
- if (points != null) {
- for (Point pt : points) {
- if (pt.x < min_x)
- min_x = pt.x;
- if (pt.x > max_x)
- max_x = pt.x;
- if (pt.y < min_y)
- min_y = pt.y;
- if (pt.y > max_y)
- max_y = pt.y;
- }
- return new Rectangle(min_x, min_y, max_x - min_x, max_y - min_y);
- }
- return null;
- }
- public static Point getAbsoluteCenter(Point[] points) {
- Rectangle rect = getBoundingBox(points);
- return new Point((int) rect.getCenterX(), (int) rect.getCenterY());
- }
- public static Ellipse2D getBoundingEllipse(Point[] points) {
- Rectangle bound = getBoundingBox(points);
- return new Ellipse2D.Double(bound.x, bound.y, bound.width, bound.height);
- }
- public static Polygon getPolygonNearestTo(Polygon[] polygons, Point base) {
- double bDist = Double.MAX_VALUE;
- Polygon bPoly = null;
- for (Polygon tri : polygons) {
- double dist = Point.distance(tri.getBounds().getCenterX(), tri
- .getBounds().getCenterY(), base.x, base.y);
- if (dist < bDist) {
- bDist = dist;
- bPoly = tri;
- }
- }
- return bPoly;
- }
- public static Polygon getPolygonOnModel(RSModel m) {
- if (m != null && m.getTriangles() != null)
- return getPolygonNearestTo(m.getTriangles(), getPointOnEllipse(
- getBoundingEllipse(m.getPoints())));
- return null;
- }
- public static Point getPointOnEllipse(Ellipse2D ellipse) {
- return getPointOnEllipse(ellipse,
- (int) randomLinearDistribution(0, 360));
- }
- public static Point getPointOnEllipse(Ellipse2D ellipse, int degrees) {
- return getPointOnEllipse(ellipse, degrees,
- randomNormalDistribution(0, getRadiusAt(ellipse, degrees)));
- }
- public static Point getPointOnEllipse(Ellipse2D ellipse, int degrees,
- double distFromCenter) {
- while (degrees >= 360)
- degrees -= 360;
- while (degrees < 0)
- degrees += 360;
- double sinVal = getSin(degrees), cosVal = getCos(degrees);
- return new Point(
- (int) (ellipse.getCenterX() + Math.round(distFromCenter
- * cosVal)),
- (int) (ellipse.getCenterY() + Math.round(distFromCenter
- * sinVal)));
- }
- public static Point getPointOnPolygon(Polygon p) {
- Point result = null;
- Ellipse2D boundingEllipse = getBoundingEllipse(getPoints(p));
- for (byte b = 0; b < 100 && (result == null || !p.contains(result)); b++)
- result = getPointOnEllipse(boundingEllipse);
- if (result != null)
- return result;
- else
- return new Point((int) p.getBounds().getCenterX(), (int) p
- .getBounds().getCenterY());
- }
- public static double getRadiusAt(Ellipse2D ellipse, int degrees) {
- while (degrees >= 360)
- degrees -= 360;
- while (degrees < 0)
- degrees += 360;
- if (degrees > 180)
- degrees = 360 - degrees;
- if (degrees > 90)
- degrees = 180 - degrees;
- double weightX = degrees / 90d, weightY = Math.abs(1 - weightX);
- return (weightX * (ellipse.getWidth() / 2d) + weightY
- * (ellipse.getHeight() / 2d));
- }
- public static Point[] getPoints(Polygon p) {
- Point[] result = new Point[p.npoints];
- for (int pIdx = 0; pIdx < p.npoints; pIdx++)
- result[pIdx] = new Point(p.xpoints[pIdx], p.ypoints[pIdx]);
- return result;
- }
- public static Point getPointOnModel(RSModel model) {
- if (model != null)
- return getPointOnPolygon(getPolygonOnModel(model));
- return null;
- }
- public static double getSin(int degrees) {
- if (sinTable != null && sinTable.containsKey(degrees))
- return sinTable.get(degrees);
- else {
- double sinVal = Math.sin(degrees);
- if (sinTable != null)
- sinTable.put(degrees, sinVal);
- return sinVal;
- }
- }
- public static double getCos(int degrees) {
- if (cosTable != null && cosTable.containsKey(degrees))
- return cosTable.get(degrees);
- else {
- double cosVal = Math.cos(degrees);
- if (cosTable != null)
- cosTable.put(degrees, cosVal);
- return cosVal;
- }
- }
- public static double distanceBetween(Point a, Point b) {
- return Point.distance(a.x, a.y, b.x, b.y);
- }
- final Mouse mouse;
- final Menu menu;
- final int lowerMouseSpeed, upperMouseSpeed;
- final Calculations calc;
- final Tiles tiles;
- public ModelMouse(Tiles tiles, Calculations calc, Mouse mouse, Menu menu,
- int lowerMouseSpeed, int upperMouseSpeed) {
- this.mouse = mouse;
- this.menu = menu;
- this.lowerMouseSpeed = lowerMouseSpeed;
- this.upperMouseSpeed = upperMouseSpeed;
- this.calc = calc;
- this.tiles = tiles;
- }
- // Models
- /**
- * Clicks the RSModel and clicks the menu action
- *
- * @param model
- * The RSModel
- * @param action
- * the action to be clicked in the menu
- * @return true if clicked, false if failed.
- */
- public boolean doAction(RSModel model, String action) {
- if (model != null && model.getPointOnScreen() != null) {
- Point modelPoint = getPointOnModel(model);
- if (modelPoint != null) {
- mouse.setSpeed((int) randomLinearDistribution(lowerMouseSpeed,
- upperMouseSpeed));
- mouse.move(modelPoint);
- return menu.doAction(action);
- }
- }
- return false;
- }
- /**
- * Clicks the RSModel.
- *
- * @param model
- * the RSModel
- * @param leftClick
- * if true it left clicks.
- * @return true if clicked.
- */
- public boolean doClick(RSModel model, boolean leftClick) {
- if (model != null && model.getPointOnScreen() != null) {
- Point modelPoint = getPointOnModel(model);
- if (modelPoint != null) {
- mouse.setSpeed((int) randomLinearDistribution(lowerMouseSpeed,
- upperMouseSpeed));
- mouse.move(modelPoint);
- mouse.click(leftClick);
- return true;
- }
- }
- return false;
- }
- /**
- * Left clicks the RSModel.
- *
- * @param model
- * the RSModel
- * @return true if clicked.
- */
- public boolean doClick(RSModel model) {
- return doClick(model, true);
- }
- /**
- * Moves the mouse onto the RSModel.
- *
- * @param model
- * The RSModel
- */
- public boolean doHover(RSModel model) {
- if (model != null && model.getPointOnScreen() != null) {
- Point modelPoint = getPointOnModel(model);
- if (modelPoint != null) {
- mouse.setSpeed((int) randomLinearDistribution(lowerMouseSpeed,
- upperMouseSpeed));
- mouse.move(modelPoint);
- return true;
- }
- }
- return false;
- }
- // Objects
- /**
- * Performs the specified action on the object.
- *
- * @param object
- * RSObject
- * @param action
- * the menu item to search and click
- * @return returns true if clicked, false if object does not contain the
- * desired action
- */
- public boolean doAction(RSObject object, String action) {
- RSModel model = object.getModel();
- if (model != null) {
- return doAction(model, action);
- }
- return tiles.doAction(object.getLocation(), action);
- }
- /**
- * Left-clicks the object.
- *
- * @param object
- * The RSObject
- * @return <tt>true</tt> if clicked.
- */
- public boolean doClick(RSObject object) {
- return doClick(object, true);
- }
- /**
- * Clicks the object.
- *
- * @param object
- * The RSObject
- * @param left
- * <tt>true</tt> to left-click; <tt>false</tt> to right-click.
- * @return <tt>true</tt> if clicked.
- */
- public boolean doClick(RSObject object, boolean leftClick) {
- RSModel model = object.getModel();
- if (model != null) {
- return doClick(model, leftClick);
- } else {
- Point p = calc.tileToScreen(object.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- if (calc.pointOnScreen(p)) {
- mouse.click(leftClick);
- return true;
- } else {
- p = calc.tileToScreen(object.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- mouse.click(leftClick);
- return true;
- }
- }
- }
- return false;
- }
- }
- /**
- * Moves the mouse over the object.
- *
- * @param object
- * The RSObject
- * @return <tt>true</tt> if the mouse was moved.
- */
- public boolean doHover(RSObject object) {
- RSModel model = object.getModel();
- if (model != null) {
- return doHover(model);
- } else {
- Point p = calc.tileToScreen(object.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- return true;
- }
- }
- return false;
- }
- // Characters
- /**
- * Performs the specified action on the character.
- *
- * @param character
- * RSCharacter
- * @param action
- * the menu item to search and click
- * @return returns true if clicked, false if character does not contain the
- * desired action
- */
- public boolean doAction(RSCharacter character, String action) {
- RSModel model = character.getModel();
- if (model != null) {
- return doAction(model, action);
- }
- return tiles.doAction(character.getLocation(), action);
- }
- /**
- * Left-clicks the character.
- *
- * @param character
- * The RSCharacter
- * @return <tt>true</tt> if clicked.
- */
- public boolean doClick(RSCharacter character) {
- return doClick(character, true);
- }
- /**
- * Clicks the character.
- *
- * @param character
- * The RSCharacter
- * @param left
- * <tt>true</tt> to left-click; <tt>false</tt> to right-click.
- * @return <tt>true</tt> if clicked.
- */
- public boolean doClick(RSCharacter character, boolean leftClick) {
- RSModel model = character.getModel();
- if (model != null) {
- return doClick(model, leftClick);
- } else {
- Point p = calc.tileToScreen(character.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- if (calc.pointOnScreen(p)) {
- mouse.click(leftClick);
- return true;
- } else {
- p = calc.tileToScreen(character.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- mouse.click(leftClick);
- return true;
- }
- }
- }
- return false;
- }
- }
- /**
- * Moves the mouse over the character.
- *
- * @param character
- * The RSCharacter
- * @return <tt>true</tt> if the mouse was moved.
- */
- public boolean doHover(RSCharacter character) {
- RSModel model = character.getModel();
- if (model != null) {
- return doHover(model);
- } else {
- Point p = calc.tileToScreen(character.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- return true;
- }
- }
- return false;
- }
- // Ground Items
- /**
- * Performs the specified action on the Ground Item.
- *
- * @param groundItem
- * RSGroundItem
- * @param action
- * the menu item to search and click
- * @return returns true if clicked, false if groundItem does not contain the
- * desired action
- */
- public boolean doAction(RSGroundItem groundItem, String action) {
- RSModel model = groundItem.getModel();
- if (model != null) {
- return doAction(model, action);
- }
- return tiles.doAction(groundItem.getLocation(), action);
- }
- /**
- * Left-clicks the Ground Item.
- *
- * @param groundItem
- * The RSGroundItem
- * @return <tt>true</tt> if clicked.
- */
- public boolean doClick(RSGroundItem groundItem) {
- return doClick(groundItem, true);
- }
- /**
- * Clicks the Ground Item.
- *
- * @param groundItem
- * The RSGroundItem
- * @param left
- * <tt>true</tt> to left-click; <tt>false</tt> to right-click.
- * @return <tt>true</tt> if clicked.
- */
- public boolean doClick(RSGroundItem groundItem, boolean leftClick) {
- RSModel model = groundItem.getModel();
- if (model != null) {
- return doClick(model, leftClick);
- } else {
- Point p = calc.tileToScreen(groundItem.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- if (calc.pointOnScreen(p)) {
- mouse.click(leftClick);
- return true;
- } else {
- p = calc.tileToScreen(groundItem.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- mouse.click(leftClick);
- return true;
- }
- }
- }
- return false;
- }
- }
- /**
- * Moves the mouse over the Ground Item.
- *
- * @param groundItem
- * The RSGroundItem
- * @return <tt>true</tt> if the mouse was moved.
- */
- public boolean doHover(RSGroundItem groundItem) {
- RSModel model = groundItem.getModel();
- if (model != null) {
- return doHover(model);
- } else {
- Point p = calc.tileToScreen(groundItem.getLocation());
- if (calc.pointOnScreen(p)) {
- mouse.move(p);
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment