Equinox

ModelMouse.java

Mar 20th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.95 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.awt.Polygon;
  3. import java.awt.Rectangle;
  4. import java.awt.geom.Ellipse2D;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Random;
  8.  
  9. import org.rsbot.script.methods.Calculations;
  10. import org.rsbot.script.methods.Menu;
  11. import org.rsbot.script.methods.Mouse;
  12. import org.rsbot.script.methods.Tiles;
  13. import org.rsbot.script.wrappers.RSCharacter;
  14. import org.rsbot.script.wrappers.RSGroundItem;
  15. import org.rsbot.script.wrappers.RSModel;
  16. import org.rsbot.script.wrappers.RSObject;
  17.  
  18. /**
  19.  * A class for clicking on objects via their model. Uses normally distributed
  20.  * points inside bounding ellipses
  21.  *
  22.  * @author Equinox_
  23.  *
  24.  */
  25. public class ModelMouse {
  26.     // Sin & Cos Tables
  27.     static Map<Integer, Double> sinTable;
  28.     static Map<Integer, Double> cosTable;
  29.  
  30.     static {
  31.         cacheTrig();
  32.     }
  33.  
  34.     public static void cacheTrig() {
  35.         sinTable = new HashMap<Integer, Double>();
  36.         cosTable = new HashMap<Integer, Double>();
  37.     }
  38.  
  39.     public static void unCacheTrig() {
  40.         sinTable = null;
  41.         cosTable = null;
  42.     }
  43.  
  44.     public static double randomLinearDistribution(double min, double max) {
  45.         return Math.min(min, max) + new Random().nextDouble()
  46.                 * Math.abs(max - min);
  47.     }
  48.  
  49.     public static double randomNormalDistribution(double MIN, double MAX) {
  50.         double mean = MIN + (MAX - MIN) / 2d;
  51.         double rand;
  52.         double sd = (MAX - MIN) / 3d;
  53.         do {
  54.             rand = (new Random().nextGaussian() * sd + mean);
  55.         } while (rand < MIN || rand >= MAX);
  56.         return rand;
  57.     }
  58.  
  59.     public static Rectangle getBoundingBox(Point[] points) {
  60.         int min_x = Integer.MAX_VALUE;
  61.         int min_y = Integer.MAX_VALUE;
  62.         int max_x = Integer.MIN_VALUE;
  63.         int max_y = Integer.MIN_VALUE;
  64.         if (points != null) {
  65.             for (Point pt : points) {
  66.                 if (pt.x < min_x)
  67.                     min_x = pt.x;
  68.                 if (pt.x > max_x)
  69.                     max_x = pt.x;
  70.                 if (pt.y < min_y)
  71.                     min_y = pt.y;
  72.                 if (pt.y > max_y)
  73.                     max_y = pt.y;
  74.             }
  75.             return new Rectangle(min_x, min_y, max_x - min_x, max_y - min_y);
  76.         }
  77.         return null;
  78.     }
  79.  
  80.     public static Point getAbsoluteCenter(Point[] points) {
  81.         Rectangle rect = getBoundingBox(points);
  82.         return new Point((int) rect.getCenterX(), (int) rect.getCenterY());
  83.     }
  84.  
  85.     public static Ellipse2D getBoundingEllipse(Point[] points) {
  86.         Rectangle bound = getBoundingBox(points);
  87.         return new Ellipse2D.Double(bound.x, bound.y, bound.width, bound.height);
  88.     }
  89.  
  90.     public static Polygon getPolygonNearestTo(Polygon[] polygons, Point base) {
  91.         double bDist = Double.MAX_VALUE;
  92.         Polygon bPoly = null;
  93.         for (Polygon tri : polygons) {
  94.             double dist = Point.distance(tri.getBounds().getCenterX(), tri
  95.                     .getBounds().getCenterY(), base.x, base.y);
  96.             if (dist < bDist) {
  97.                 bDist = dist;
  98.                 bPoly = tri;
  99.             }
  100.         }
  101.         return bPoly;
  102.     }
  103.  
  104.     public static Polygon getPolygonOnModel(RSModel m) {
  105.         if (m != null && m.getTriangles() != null)
  106.             return getPolygonNearestTo(m.getTriangles(), getPointOnEllipse(
  107.                     getBoundingEllipse(m.getPoints())));
  108.         return null;
  109.     }
  110.  
  111.     public static Point getPointOnEllipse(Ellipse2D ellipse) {
  112.         return getPointOnEllipse(ellipse,
  113.                 (int) randomLinearDistribution(0, 360));
  114.     }
  115.  
  116.     public static Point getPointOnEllipse(Ellipse2D ellipse, int degrees) {
  117.         return getPointOnEllipse(ellipse, degrees,
  118.                 randomNormalDistribution(0, getRadiusAt(ellipse, degrees)));
  119.     }
  120.  
  121.     public static Point getPointOnEllipse(Ellipse2D ellipse, int degrees,
  122.             double distFromCenter) {
  123.         while (degrees >= 360)
  124.             degrees -= 360;
  125.         while (degrees < 0)
  126.             degrees += 360;
  127.         double sinVal = getSin(degrees), cosVal = getCos(degrees);
  128.         return new Point(
  129.                 (int) (ellipse.getCenterX() + Math.round(distFromCenter
  130.                         * cosVal)),
  131.                 (int) (ellipse.getCenterY() + Math.round(distFromCenter
  132.                         * sinVal)));
  133.     }
  134.  
  135.     public static Point getPointOnPolygon(Polygon p) {
  136.         Point result = null;
  137.         Ellipse2D boundingEllipse = getBoundingEllipse(getPoints(p));
  138.         for (byte b = 0; b < 100 && (result == null || !p.contains(result)); b++)
  139.             result = getPointOnEllipse(boundingEllipse);
  140.         if (result != null)
  141.             return result;
  142.         else
  143.             return new Point((int) p.getBounds().getCenterX(), (int) p
  144.                     .getBounds().getCenterY());
  145.     }
  146.  
  147.     public static double getRadiusAt(Ellipse2D ellipse, int degrees) {
  148.         while (degrees >= 360)
  149.             degrees -= 360;
  150.         while (degrees < 0)
  151.             degrees += 360;
  152.         if (degrees > 180)
  153.             degrees = 360 - degrees;
  154.         if (degrees > 90)
  155.             degrees = 180 - degrees;
  156.         double weightX = degrees / 90d, weightY = Math.abs(1 - weightX);
  157.         return (weightX * (ellipse.getWidth() / 2d) + weightY
  158.                 * (ellipse.getHeight() / 2d));
  159.     }
  160.  
  161.     public static Point[] getPoints(Polygon p) {
  162.         Point[] result = new Point[p.npoints];
  163.         for (int pIdx = 0; pIdx < p.npoints; pIdx++)
  164.             result[pIdx] = new Point(p.xpoints[pIdx], p.ypoints[pIdx]);
  165.         return result;
  166.     }
  167.  
  168.     public static Point getPointOnModel(RSModel model) {
  169.         if (model != null)
  170.             return getPointOnPolygon(getPolygonOnModel(model));
  171.         return null;
  172.     }
  173.  
  174.     public static double getSin(int degrees) {
  175.         if (sinTable != null && sinTable.containsKey(degrees))
  176.             return sinTable.get(degrees);
  177.         else {
  178.             double sinVal = Math.sin(degrees);
  179.             if (sinTable != null)
  180.                 sinTable.put(degrees, sinVal);
  181.             return sinVal;
  182.         }
  183.     }
  184.  
  185.     public static double getCos(int degrees) {
  186.         if (cosTable != null && cosTable.containsKey(degrees))
  187.             return cosTable.get(degrees);
  188.         else {
  189.             double cosVal = Math.cos(degrees);
  190.             if (cosTable != null)
  191.                 cosTable.put(degrees, cosVal);
  192.             return cosVal;
  193.         }
  194.     }
  195.  
  196.     public static double distanceBetween(Point a, Point b) {
  197.         return Point.distance(a.x, a.y, b.x, b.y);
  198.     }
  199.  
  200.     final Mouse mouse;
  201.     final Menu menu;
  202.     final int lowerMouseSpeed, upperMouseSpeed;
  203.     final Calculations calc;
  204.     final Tiles tiles;
  205.  
  206.     public ModelMouse(Tiles tiles, Calculations calc, Mouse mouse, Menu menu,
  207.             int lowerMouseSpeed, int upperMouseSpeed) {
  208.         this.mouse = mouse;
  209.         this.menu = menu;
  210.         this.lowerMouseSpeed = lowerMouseSpeed;
  211.         this.upperMouseSpeed = upperMouseSpeed;
  212.         this.calc = calc;
  213.         this.tiles = tiles;
  214.     }
  215.  
  216.     // Models
  217.     /**
  218.      * Clicks the RSModel and clicks the menu action
  219.      *
  220.      * @param model
  221.      *            The RSModel
  222.      * @param action
  223.      *            the action to be clicked in the menu
  224.      * @return true if clicked, false if failed.
  225.      */
  226.     public boolean doAction(RSModel model, String action) {
  227.         if (model != null && model.getPointOnScreen() != null) {
  228.             Point modelPoint = getPointOnModel(model);
  229.             if (modelPoint != null) {
  230.                 mouse.setSpeed((int) randomLinearDistribution(lowerMouseSpeed,
  231.                         upperMouseSpeed));
  232.                 mouse.move(modelPoint);
  233.                 return menu.doAction(action);
  234.             }
  235.         }
  236.         return false;
  237.     }
  238.  
  239.     /**
  240.      * Clicks the RSModel.
  241.      *
  242.      * @param model
  243.      *            the RSModel
  244.      * @param leftClick
  245.      *            if true it left clicks.
  246.      * @return true if clicked.
  247.      */
  248.     public boolean doClick(RSModel model, boolean leftClick) {
  249.         if (model != null && model.getPointOnScreen() != null) {
  250.             Point modelPoint = getPointOnModel(model);
  251.             if (modelPoint != null) {
  252.                 mouse.setSpeed((int) randomLinearDistribution(lowerMouseSpeed,
  253.                         upperMouseSpeed));
  254.                 mouse.move(modelPoint);
  255.                 mouse.click(leftClick);
  256.                 return true;
  257.             }
  258.         }
  259.         return false;
  260.     }
  261.  
  262.     /**
  263.      * Left clicks the RSModel.
  264.      *
  265.      * @param model
  266.      *            the RSModel
  267.      * @return true if clicked.
  268.      */
  269.     public boolean doClick(RSModel model) {
  270.         return doClick(model, true);
  271.     }
  272.  
  273.     /**
  274.      * Moves the mouse onto the RSModel.
  275.      *
  276.      * @param model
  277.      *            The RSModel
  278.      */
  279.     public boolean doHover(RSModel model) {
  280.         if (model != null && model.getPointOnScreen() != null) {
  281.             Point modelPoint = getPointOnModel(model);
  282.             if (modelPoint != null) {
  283.                 mouse.setSpeed((int) randomLinearDistribution(lowerMouseSpeed,
  284.                         upperMouseSpeed));
  285.                 mouse.move(modelPoint);
  286.                 return true;
  287.             }
  288.         }
  289.         return false;
  290.     }
  291.  
  292.     // Objects
  293.     /**
  294.      * Performs the specified action on the object.
  295.      *
  296.      * @param object
  297.      *            RSObject
  298.      * @param action
  299.      *            the menu item to search and click
  300.      * @return returns true if clicked, false if object does not contain the
  301.      *         desired action
  302.      */
  303.     public boolean doAction(RSObject object, String action) {
  304.  
  305.         RSModel model = object.getModel();
  306.         if (model != null) {
  307.             return doAction(model, action);
  308.         }
  309.         return tiles.doAction(object.getLocation(), action);
  310.     }
  311.  
  312.     /**
  313.      * Left-clicks the object.
  314.      *
  315.      * @param object
  316.      *            The RSObject
  317.      * @return <tt>true</tt> if clicked.
  318.      */
  319.     public boolean doClick(RSObject object) {
  320.         return doClick(object, true);
  321.     }
  322.  
  323.     /**
  324.      * Clicks the object.
  325.      *
  326.      * @param object
  327.      *            The RSObject
  328.      * @param left
  329.      *            <tt>true</tt> to left-click; <tt>false</tt> to right-click.
  330.      * @return <tt>true</tt> if clicked.
  331.      */
  332.     public boolean doClick(RSObject object, boolean leftClick) {
  333.         RSModel model = object.getModel();
  334.         if (model != null) {
  335.             return doClick(model, leftClick);
  336.         } else {
  337.             Point p = calc.tileToScreen(object.getLocation());
  338.             if (calc.pointOnScreen(p)) {
  339.                 mouse.move(p);
  340.                 if (calc.pointOnScreen(p)) {
  341.                     mouse.click(leftClick);
  342.                     return true;
  343.                 } else {
  344.                     p = calc.tileToScreen(object.getLocation());
  345.                     if (calc.pointOnScreen(p)) {
  346.                         mouse.move(p);
  347.                         mouse.click(leftClick);
  348.                         return true;
  349.                     }
  350.                 }
  351.             }
  352.             return false;
  353.         }
  354.     }
  355.  
  356.     /**
  357.      * Moves the mouse over the object.
  358.      *
  359.      * @param object
  360.      *            The RSObject
  361.      * @return <tt>true</tt> if the mouse was moved.
  362.      */
  363.     public boolean doHover(RSObject object) {
  364.         RSModel model = object.getModel();
  365.         if (model != null) {
  366.             return doHover(model);
  367.         } else {
  368.             Point p = calc.tileToScreen(object.getLocation());
  369.             if (calc.pointOnScreen(p)) {
  370.                 mouse.move(p);
  371.                 return true;
  372.             }
  373.         }
  374.         return false;
  375.     }
  376.  
  377.     // Characters
  378.     /**
  379.      * Performs the specified action on the character.
  380.      *
  381.      * @param character
  382.      *            RSCharacter
  383.      * @param action
  384.      *            the menu item to search and click
  385.      * @return returns true if clicked, false if character does not contain the
  386.      *         desired action
  387.      */
  388.     public boolean doAction(RSCharacter character, String action) {
  389.         RSModel model = character.getModel();
  390.         if (model != null) {
  391.             return doAction(model, action);
  392.         }
  393.         return tiles.doAction(character.getLocation(), action);
  394.     }
  395.  
  396.     /**
  397.      * Left-clicks the character.
  398.      *
  399.      * @param character
  400.      *            The RSCharacter
  401.      * @return <tt>true</tt> if clicked.
  402.      */
  403.     public boolean doClick(RSCharacter character) {
  404.         return doClick(character, true);
  405.     }
  406.  
  407.     /**
  408.      * Clicks the character.
  409.      *
  410.      * @param character
  411.      *            The RSCharacter
  412.      * @param left
  413.      *            <tt>true</tt> to left-click; <tt>false</tt> to right-click.
  414.      * @return <tt>true</tt> if clicked.
  415.      */
  416.     public boolean doClick(RSCharacter character, boolean leftClick) {
  417.         RSModel model = character.getModel();
  418.         if (model != null) {
  419.             return doClick(model, leftClick);
  420.         } else {
  421.             Point p = calc.tileToScreen(character.getLocation());
  422.             if (calc.pointOnScreen(p)) {
  423.                 mouse.move(p);
  424.                 if (calc.pointOnScreen(p)) {
  425.                     mouse.click(leftClick);
  426.                     return true;
  427.                 } else {
  428.                     p = calc.tileToScreen(character.getLocation());
  429.                     if (calc.pointOnScreen(p)) {
  430.                         mouse.move(p);
  431.                         mouse.click(leftClick);
  432.                         return true;
  433.                     }
  434.                 }
  435.             }
  436.             return false;
  437.         }
  438.     }
  439.  
  440.     /**
  441.      * Moves the mouse over the character.
  442.      *
  443.      * @param character
  444.      *            The RSCharacter
  445.      * @return <tt>true</tt> if the mouse was moved.
  446.      */
  447.     public boolean doHover(RSCharacter character) {
  448.         RSModel model = character.getModel();
  449.         if (model != null) {
  450.             return doHover(model);
  451.         } else {
  452.             Point p = calc.tileToScreen(character.getLocation());
  453.             if (calc.pointOnScreen(p)) {
  454.                 mouse.move(p);
  455.                 return true;
  456.             }
  457.         }
  458.         return false;
  459.     }
  460.  
  461.     // Ground Items
  462.     /**
  463.      * Performs the specified action on the Ground Item.
  464.      *
  465.      * @param groundItem
  466.      *            RSGroundItem
  467.      * @param action
  468.      *            the menu item to search and click
  469.      * @return returns true if clicked, false if groundItem does not contain the
  470.      *         desired action
  471.      */
  472.     public boolean doAction(RSGroundItem groundItem, String action) {
  473.         RSModel model = groundItem.getModel();
  474.         if (model != null) {
  475.             return doAction(model, action);
  476.         }
  477.         return tiles.doAction(groundItem.getLocation(), action);
  478.     }
  479.  
  480.     /**
  481.      * Left-clicks the Ground Item.
  482.      *
  483.      * @param groundItem
  484.      *            The RSGroundItem
  485.      * @return <tt>true</tt> if clicked.
  486.      */
  487.     public boolean doClick(RSGroundItem groundItem) {
  488.         return doClick(groundItem, true);
  489.     }
  490.  
  491.     /**
  492.      * Clicks the Ground Item.
  493.      *
  494.      * @param groundItem
  495.      *            The RSGroundItem
  496.      * @param left
  497.      *            <tt>true</tt> to left-click; <tt>false</tt> to right-click.
  498.      * @return <tt>true</tt> if clicked.
  499.      */
  500.     public boolean doClick(RSGroundItem groundItem, boolean leftClick) {
  501.         RSModel model = groundItem.getModel();
  502.         if (model != null) {
  503.             return doClick(model, leftClick);
  504.         } else {
  505.             Point p = calc.tileToScreen(groundItem.getLocation());
  506.             if (calc.pointOnScreen(p)) {
  507.                 mouse.move(p);
  508.                 if (calc.pointOnScreen(p)) {
  509.                     mouse.click(leftClick);
  510.                     return true;
  511.                 } else {
  512.                     p = calc.tileToScreen(groundItem.getLocation());
  513.                     if (calc.pointOnScreen(p)) {
  514.                         mouse.move(p);
  515.                         mouse.click(leftClick);
  516.                         return true;
  517.                     }
  518.                 }
  519.             }
  520.             return false;
  521.         }
  522.     }
  523.  
  524.     /**
  525.      * Moves the mouse over the Ground Item.
  526.      *
  527.      * @param groundItem
  528.      *            The RSGroundItem
  529.      * @return <tt>true</tt> if the mouse was moved.
  530.      */
  531.     public boolean doHover(RSGroundItem groundItem) {
  532.         RSModel model = groundItem.getModel();
  533.         if (model != null) {
  534.             return doHover(model);
  535.         } else {
  536.             Point p = calc.tileToScreen(groundItem.getLocation());
  537.             if (calc.pointOnScreen(p)) {
  538.                 mouse.move(p);
  539.                 return true;
  540.             }
  541.         }
  542.         return false;
  543.     }
  544.  
  545. }
Advertisement
Add Comment
Please, Sign In to add comment