Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kbot2.scriptable.methods.input;
- import com.kbot2.bot.BotEnvironment;
- import static com.kbot2.scriptable.methods.Calculations.random;
- import com.kbot2.scriptable.methods.interfaces.ScreenObject;
- import com.kbot2.handlers.settings.BotConstants;
- import com.kbot2.handlers.eventSystem.eventListeners.PaintListener;
- import com.kbot2.accessors.Client;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.geom.Point2D;
- import java.awt.image.BufferedImage;
- import java.util.Random;
- import java.util.LinkedList;
- import java.applet.Applet;
- import org.jvnet.lafwidget.UiThreadingViolationException;
- /**
- * Created by IntelliJ IDEA.
- * User: Jan Ove / Kosaki
- * Date: 22.mar.2009
- * Time: 15:59:15
- */
- public class Mouse implements PaintListener {
- private final BotEnvironment botEnv;
- private Canvas canvas;
- private final BufferedImage buffImage;
- private final Graphics2D buffGraphics;
- private int n = 4;
- private final int w;
- private final int h;
- private DoublePoint[] points;
- private final Random random = new Random();
- public final java.util.List<Line> splineLines = new LinkedList<Line>();
- private final java.util.List<Line> outlineLines = new LinkedList<Line>();
- private final java.util.List<Point> outLinePoints = new LinkedList<Point>();
- public Mouse(BotEnvironment botEnv) {
- this.botEnv = botEnv;
- w = 765;
- h = 503;
- buffImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- buffGraphics = buffImage.createGraphics();
- buffGraphics.setBackground(new Color(0, 0, 0, 0));
- botEnv.botInstance.getEventHandler().addPaintListener(this);
- }
- private double getSpeed(int percentage) {
- /**
- * Function for speed:
- * f(x) = 0.001x^2+0.01x
- */
- double a = random.nextDouble() * 0.01 + 0.0001;
- double b = random.nextDouble() * 0.03 + 0.005;
- double speedFactor = a * Math.pow(percentage, 2) + b * percentage;
- return botEnv.mouseSpeed + speedFactor;
- }
- private double speed = BotConstants.mouseSpeed;
- private void splineMouse() {
- Line line1 = splineLines.get(0);
- int lastX = line1.x1;
- int lastY = line1.y1;
- if (line1.x1 < 0 || line1.y1 < 0 || line1.x1 > 766 || line1.y1 > 504) {
- lastX = -1;
- lastY = -1;
- }
- int x;
- int y;
- for (int i = 0; i < splineLines.size(); i++) {
- int percentage = i / splineLines.size();
- speed = getSpeed(percentage);
- Line line = splineLines.get(i);
- x = line.x2;
- y = line.y2;
- if (lastX != x || lastY != y &&
- (x > -1 && y > -1 && x < 766 && y < 504)) {
- moveMouseInternal(lastX, lastY, x, y);
- lastX = x;
- lastY = y;
- } else {
- if (lastX != x || lastY != y) {
- try {
- int secs = (int) speed;
- double nanos = speed - secs;
- int nanosReal = (int) (nanos * 1000);
- Thread.sleep(secs, nanosReal);
- } catch (InterruptedException ignored) {
- }
- }
- }
- }
- buffGraphics.setComposite(AlphaComposite.Clear);
- buffGraphics.fillRect(0, 0, w, h);
- //reset alpha composite
- buffGraphics.setComposite(AlphaComposite.SrcOver);
- }
- /**
- * @param n
- * @return returns a "humanlike" control point
- * @author PwnZ
- */
- private double createSmartControlPoint(final int n, final double spacing, final boolean yValue, final int distance) {
- final int length = (int) spacing;
- double d;
- if (yValue) {
- d = random(0, length - n) * random.nextDouble() * random(0, 2);
- } else {
- d = random(0, length - n) * random.nextDouble() * random(distance / 100, distance / 100 + random(2, 4));
- }
- return d;
- }
- private void createSpline(Point start, Point end) {
- int distance = (int) Point2D.distance(start.x, start.y, end.x, end.y);
- n = (distance / 100) + random.nextInt(3) + 4;
- if (distance < 100)
- n = 3;
- points = new DoublePoint[n];
- points[0] = new DoublePoint(start.x, start.y);
- points[n - 1] = new DoublePoint(end.x, end.y);
- int midPoints = n - 2;
- DoublePoint lastPos = new DoublePoint(points[0].x, points[0].y);
- for (int i = 1; i < n - 1; i++) {
- double X = lastPos.x;
- double Y = lastPos.y;
- double spacing = distance / (midPoints + 2);
- int randomNum = random.nextInt(2);
- if (randomNum == 0) {
- X += createSmartControlPoint(i, spacing, false, distance)/**(random.nextInt(2) == 0? -1:1)*/;
- } else {
- X -= createSmartControlPoint(i, spacing, false, distance)/**(random.nextInt(2) == 0? -1:1)*/;
- }
- randomNum = random.nextInt(2);
- if (randomNum == 0) {
- Y += createSmartControlPoint(i, spacing, true, distance)/**(random.nextInt(2) == 0? -1:1)*/;
- } else {
- Y -= createSmartControlPoint(i, spacing, true, distance)/**(random.nextInt(2) == 0? -1:1)*/;
- }
- points[i] = new DoublePoint(X, Y);
- lastPos.x = X;
- lastPos.y = Y;
- }
- generateSpline();
- }
- public void paintSpline() {
- buffGraphics.setComposite(AlphaComposite.Clear);
- buffGraphics.fillRect(0, 0, w, h);
- //reset alpha composite
- buffGraphics.setComposite(AlphaComposite.SrcOver);
- buffGraphics.setColor(Color.blue);
- for (Point p : outLinePoints) {
- buffGraphics.drawRect(p.x - 2, p.y - 2, 4, 4);
- }
- for (Line l : outlineLines) {
- buffGraphics.drawLine(l.x1, l.y1, l.x2, l.y2);
- }
- buffGraphics.setColor(Color.red);
- for (Line l : splineLines) {
- buffGraphics.drawLine(l.x1, l.y1, l.x2, l.y2);
- }
- }
- private void generateSpline() {
- outlineLines.clear();
- outLinePoints.clear();
- splineLines.clear();
- double step = 1. / w, t = step;
- DoublePoint[] points2 = new DoublePoint[n];
- int X, Y, Xold = (int) points[0].x, Yold = (int) points[0].y;
- for (int i = 0; i < n; i++) {
- X = (int) points[i].x;
- Y = (int) points[i].y;
- outLinePoints.add(new Point(X, Y));
- }
- if (n > 2) {
- int Xo = Xold, Yo = Yold;
- for (int i = 1; i < n; i++) {
- X = (int) points[i].x;
- Y = (int) points[i].y;
- if ((Xo < X) && (Yo < Y)) {
- if (random(1, 4)== 2) {
- X += random(0, 1);
- Y += random(1, 2);
- } else if (random(1, 4)== 2) {
- X += random(1, 2);
- Y += random(0, 1);
- }
- } else if ((Xo < X) && (Yo > Y)) {
- if (random(1, 4)== 2) {
- X += random(0, 1);
- Y -= random(1, 2);
- } else if (random(1, 4)== 2) {
- X += random(1, 2);
- Y -= random(0, 1);
- }
- } else if ((Xo > X) && (Yo < Y)) {
- if (random(1, 2)== 4) {
- X -= random(0, 1);
- Y += random(1, 2);
- } else if (random(1, 4)== 2) {
- X -= random(1, 2);
- Y += random(0, 1);
- }
- } else if ((Xo > X) && (Yo > Y)) {
- if (random(1, 2)== 2) {
- X -= random(0, 1);
- Y -= random(1, 2);
- } else if (random(1, 4)== 2) {
- X -= random(1, 2);
- Y -= random(0, 1);
- }
- }
- outlineLines.add(new Line(Xo, Yo, X, Y));
- Xo = X;
- Yo = Y;
- }
- }
- for (int k = 1; k < w; k++) {
- System.arraycopy(points, 0, points2, 0, n);
- for (int j = n - 1; j > 0; j--) // points calculation
- for (int i = 0; i < j; i++) {
- points2[i].x = (1 - t) * points2[i].x + t * points2[i + 1].x;
- points2[i].y = (1 - t) * points2[i].y + t * points2[i + 1].y;
- }
- X = (int) points2[0].x;
- Y = (int) points2[0].y;
- if ((Xold < X) && (Yold < Y)) {
- if (random(1, 4)== 2) {
- X += random(0, 1);
- Y += random(1, 2);
- } else if (random(1, 4)== 2) {
- X += random(1, 2);
- Y += random(0, 1);
- }
- } else if ((Xold < X) && (Yold > Y)) {
- if (random(1, 4)== 2) {
- X += random(0, 1);
- Y -= random(1, 2);
- } else if (random(1, 4)== 2) {
- X += random(1, 2);
- Y -= random(0, 1);
- }
- } else if ((Xold > X) && (Yold < Y)) {
- if (random(1, 2)== 4) {
- X -= random(0, 1);
- Y += random(1, 2);
- } else if (random(1, 4)== 2) {
- X -= random(1, 2);
- Y += random(0, 1);
- }
- } else if ((Xold > X) && (Yold > Y)) {
- if (random(1, 2)== 2) {
- X -= random(0, 1);
- Y -= random(1, 2);
- } else if (random(1, 4)== 2) {
- X -= random(1, 2);
- Y -= random(0, 1);
- }
- }
- splineLines.add(new Line(Xold, Yold, X, Y));
- Xold = X;
- Yold = Y;
- t += step;
- }
- paintSpline();
- }
- public void setMousePos(int x, int y) {
- if (x < 0) {
- x = -1;
- }
- if (y < 0) {
- y = -1;
- }
- if (x > 765) {
- x = -1;
- }
- if (y > 504)
- y = -1;
- getClient().getMouse().dispatchEvent(new MouseEvent((Applet) getClient(), MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, x, y, 0, false, 0));
- }
- private Canvas getCanvas() {
- if (canvas == null) {
- canvas = getClient().getCanvas();
- }
- return canvas;
- }
- private void moveMouseInternal(int curX, int curY, int x, int y) {
- double distance = Point2D.distance(curX, curY, x, y);
- while (distance > 0) {
- if (curX < 0 || curY < 0) {
- curX = x;
- curY = y;
- }
- if (Math.round(curX) < Math.round(x))
- curX++;
- else if (Math.round(curX) > Math.round(x))
- curX--;
- if (Math.round(curY) < Math.round(y))
- curY++;
- else if (Math.round(curY) > Math.round(y))
- curY--;
- setMousePos(curX, curY);
- try {
- int secs = (int) speed;
- double nanos = speed - secs;
- int nanosReal = (int) (nanos * 1000);
- Thread.sleep(secs, nanosReal);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- distance = Point2D.distance(curX, curY, x, y);
- }
- }
- public void moveMouse(int x, int y, int randomX, int randomY) {
- Client client = getClient();
- int thisX = client.getMouseX(), thisY = client.getMouseY();
- if (thisX < 0 || thisY < 0) {
- switch (random(1, 5)) { // on which side of canvas should it enter
- case 1:
- thisX = 1;
- thisY = random(0, 500);
- setMousePos(thisX, thisY);
- break;
- case 2:
- thisX = random(0, 765);
- thisY = 501;
- setMousePos(thisX, thisY);
- break;
- case 3:
- thisX = 766;
- thisY = random(0, 500);
- setMousePos(thisX, thisY);
- break;
- case 4:
- thisX = random(0, 765);
- thisY = 1;
- setMousePos(thisX, thisY);
- break;
- }
- }
- if (thisX == x && thisY == y) {
- return;
- }
- if (Point2D.distanceSq(thisX, thisY, x, y) < 10) {
- splineLines.clear();
- splineLines.add(new Line(thisX, thisY, random(x, x + randomX), random(y, y + randomY)));
- paintSpline();
- } else {
- createSpline(new Point(thisX, thisY), new Point(random(x, x + randomX), random(y, y + randomY)));
- }
- splineMouse();
- }
- public void moveMouse(int x, int y) {
- moveMouse(x, y, 0, 0);
- }
- public void pressMouse(int x, int y, boolean button) {
- if (x < 0 || y < 0 || x > 756 || y > 503)
- return;
- try {
- getClient().getMouse().dispatchEvent(new MouseEvent((Applet) getClient(),
- MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), 0, x, y,
- 1, false, button ? MouseEvent.BUTTON1 : MouseEvent.BUTTON3));
- } catch (UiThreadingViolationException ignored) {
- }
- }
- public void releaseMouse(int x, int y, boolean button) {
- if (x < 0 || y < 0 || x > 756 || y > 503)
- return;
- try {
- Client client = getClient();
- getClient().getMouse().dispatchEvent(new MouseEvent((Applet) client,
- MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), 0, x, y,
- 1, false, button ? MouseEvent.BUTTON1 : MouseEvent.BUTTON3));
- getClient().getMouse().dispatchEvent(new MouseEvent((Applet) client,
- MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), 0, x,
- y, 1, false, button ? MouseEvent.BUTTON1
- : MouseEvent.BUTTON3));
- } catch (UiThreadingViolationException ignored) {
- }
- }
- /**
- * Moves the mouse with randomness and clicks.
- *
- * @param p
- * @param randomX
- * @param randomY
- * @param button
- */
- public void clickMouse(Point p, int randomX, int randomY, boolean button) {
- moveMouse(p, randomX, randomY);
- botEnv.methods.sleep(random(50, 150));
- clickMouse(button);
- }
- /**
- * Moves the mouse and clicks at the given position.
- *
- * @param p
- * @param button
- */
- public void clickMouse(Point p, boolean button) {
- clickMouse(p, 0, 0, button);
- }
- /**
- * Moves the mouse with randomness
- *
- * @param p
- * @param randomX
- * @param randomY
- */
- public void moveMouse(Point p, int randomX, int randomY) {
- moveMouse(p.x, p.y, randomX, randomY);
- }
- private void clickMouse(int x, int y, boolean button) {
- pressMouse(x, y, button);
- botEnv.methods.sleep(random(0, 70));
- releaseMouse(x, y, button);
- }
- public void clickMouse(boolean button) {
- Client client = getClient();
- clickMouse(client.getMouseX(), client.getMouseY(), button);
- }
- public Client getClient() {
- return botEnv.botInstance.getClient();
- }
- public void onRepaint(Graphics g) {
- if (BotConstants.paintMousePaths)
- g.drawImage(buffImage, 0, 0, null);
- }
- public void setMouseSpeed(double speed) {
- botEnv.mouseSpeed = speed;
- }
- private class DoublePoint {
- public double x;
- public double y;
- public DoublePoint(double x, double y) {
- this.x = x;
- this.y = y;
- }
- }
- public class Line {
- public final int x1;
- public final int y1;
- public final int x2;
- public final int y2;
- public Line(int x1, int y1, int x2, int y2) {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- }
- }
- public void notifyOnClose() {
- botEnv.botInstance.getEventHandler().removePaintListener(this);
- }
- public void dragMouse(Point destination, int randomX, int randomY) {
- Client client = getClient();
- int thisX = client.getMouseX(), thisY = client.getMouseY();
- pressMouse(thisX, thisY, true);
- botEnv.methods.sleep(10, 50);
- moveMouse(destination, randomX, randomY);
- thisX = client.getMouseX();
- thisY = client.getMouseY();
- botEnv.methods.sleep(10, 50);
- clickMouse(thisX, thisY, true);
- }
- public void dragMouse(Point destination){
- dragMouse(destination, 0, 0);
- }
- }
Add Comment
Please, Sign In to add comment