Guest User

Untitled

a guest
Jun 22nd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.24 KB | None | 0 0
  1. package com.kbot2.scriptable.methods.input;
  2.  
  3. import com.kbot2.bot.BotEnvironment;
  4. import static com.kbot2.scriptable.methods.Calculations.random;
  5. import com.kbot2.scriptable.methods.interfaces.ScreenObject;
  6. import com.kbot2.handlers.settings.BotConstants;
  7. import com.kbot2.handlers.eventSystem.eventListeners.PaintListener;
  8. import com.kbot2.accessors.Client;
  9.  
  10. import java.awt.*;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.geom.Point2D;
  13. import java.awt.image.BufferedImage;
  14. import java.util.Random;
  15. import java.util.LinkedList;
  16. import java.applet.Applet;
  17.  
  18. import org.jvnet.lafwidget.UiThreadingViolationException;
  19.  
  20. /**
  21. * Created by IntelliJ IDEA.
  22. * User: Jan Ove / Kosaki
  23. * Date: 22.mar.2009
  24. * Time: 15:59:15
  25. */
  26. public class Mouse implements PaintListener {
  27. private final BotEnvironment botEnv;
  28. private Canvas canvas;
  29. private final BufferedImage buffImage;
  30. private final Graphics2D buffGraphics;
  31. private int n = 4;
  32. private final int w;
  33. private final int h;
  34. private DoublePoint[] points;
  35. private final Random random = new Random();
  36. public final java.util.List<Line> splineLines = new LinkedList<Line>();
  37. private final java.util.List<Line> outlineLines = new LinkedList<Line>();
  38. private final java.util.List<Point> outLinePoints = new LinkedList<Point>();
  39.  
  40. public Mouse(BotEnvironment botEnv) {
  41. this.botEnv = botEnv;
  42. w = 765;
  43. h = 503;
  44. buffImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  45. buffGraphics = buffImage.createGraphics();
  46. buffGraphics.setBackground(new Color(0, 0, 0, 0));
  47.  
  48. botEnv.botInstance.getEventHandler().addPaintListener(this);
  49. }
  50.  
  51. private double getSpeed(int percentage) {
  52. /**
  53. * Function for speed:
  54. * f(x) = 0.001x^2+0.01x
  55. */
  56. double a = random.nextDouble() * 0.01 + 0.0001;
  57. double b = random.nextDouble() * 0.03 + 0.005;
  58. double speedFactor = a * Math.pow(percentage, 2) + b * percentage;
  59. return botEnv.mouseSpeed + speedFactor;
  60. }
  61.  
  62. private double speed = BotConstants.mouseSpeed;
  63.  
  64. private void splineMouse() {
  65. Line line1 = splineLines.get(0);
  66. int lastX = line1.x1;
  67. int lastY = line1.y1;
  68. if (line1.x1 < 0 || line1.y1 < 0 || line1.x1 > 766 || line1.y1 > 504) {
  69. lastX = -1;
  70. lastY = -1;
  71. }
  72. int x;
  73. int y;
  74. for (int i = 0; i < splineLines.size(); i++) {
  75. int percentage = i / splineLines.size();
  76. speed = getSpeed(percentage);
  77. Line line = splineLines.get(i);
  78. x = line.x2;
  79. y = line.y2;
  80. if (lastX != x || lastY != y &&
  81. (x > -1 && y > -1 && x < 766 && y < 504)) {
  82. moveMouseInternal(lastX, lastY, x, y);
  83. lastX = x;
  84. lastY = y;
  85. } else {
  86. if (lastX != x || lastY != y) {
  87. try {
  88. int secs = (int) speed;
  89. double nanos = speed - secs;
  90. int nanosReal = (int) (nanos * 1000);
  91. Thread.sleep(secs, nanosReal);
  92. } catch (InterruptedException ignored) {
  93. }
  94. }
  95. }
  96. }
  97. buffGraphics.setComposite(AlphaComposite.Clear);
  98. buffGraphics.fillRect(0, 0, w, h);
  99. //reset alpha composite
  100. buffGraphics.setComposite(AlphaComposite.SrcOver);
  101. }
  102.  
  103. /**
  104. * @param n
  105. * @return returns a "humanlike" control point
  106. * @author PwnZ
  107. */
  108. private double createSmartControlPoint(final int n, final double spacing, final boolean yValue, final int distance) {
  109. final int length = (int) spacing;
  110. double d;
  111. if (yValue) {
  112. d = random(0, length - n) * random.nextDouble() * random(0, 2);
  113. } else {
  114. d = random(0, length - n) * random.nextDouble() * random(distance / 100, distance / 100 + random(2, 4));
  115. }
  116. return d;
  117. }
  118.  
  119. private void createSpline(Point start, Point end) {
  120. int distance = (int) Point2D.distance(start.x, start.y, end.x, end.y);
  121. n = (distance / 100) + random.nextInt(3) + 4;
  122. if (distance < 100)
  123. n = 3;
  124. points = new DoublePoint[n];
  125.  
  126. points[0] = new DoublePoint(start.x, start.y);
  127. points[n - 1] = new DoublePoint(end.x, end.y);
  128.  
  129. int midPoints = n - 2;
  130. DoublePoint lastPos = new DoublePoint(points[0].x, points[0].y);
  131. for (int i = 1; i < n - 1; i++) {
  132. double X = lastPos.x;
  133. double Y = lastPos.y;
  134. double spacing = distance / (midPoints + 2);
  135. int randomNum = random.nextInt(2);
  136. if (randomNum == 0) {
  137. X += createSmartControlPoint(i, spacing, false, distance)/**(random.nextInt(2) == 0? -1:1)*/;
  138. } else {
  139. X -= createSmartControlPoint(i, spacing, false, distance)/**(random.nextInt(2) == 0? -1:1)*/;
  140. }
  141.  
  142. randomNum = random.nextInt(2);
  143. if (randomNum == 0) {
  144. Y += createSmartControlPoint(i, spacing, true, distance)/**(random.nextInt(2) == 0? -1:1)*/;
  145. } else {
  146. Y -= createSmartControlPoint(i, spacing, true, distance)/**(random.nextInt(2) == 0? -1:1)*/;
  147. }
  148. points[i] = new DoublePoint(X, Y);
  149. lastPos.x = X;
  150. lastPos.y = Y;
  151. }
  152. generateSpline();
  153. }
  154.  
  155. public void paintSpline() {
  156. buffGraphics.setComposite(AlphaComposite.Clear);
  157. buffGraphics.fillRect(0, 0, w, h);
  158. //reset alpha composite
  159. buffGraphics.setComposite(AlphaComposite.SrcOver);
  160.  
  161.  
  162. buffGraphics.setColor(Color.blue);
  163. for (Point p : outLinePoints) {
  164. buffGraphics.drawRect(p.x - 2, p.y - 2, 4, 4);
  165. }
  166. for (Line l : outlineLines) {
  167. buffGraphics.drawLine(l.x1, l.y1, l.x2, l.y2);
  168. }
  169. buffGraphics.setColor(Color.red);
  170. for (Line l : splineLines) {
  171. buffGraphics.drawLine(l.x1, l.y1, l.x2, l.y2);
  172. }
  173. }
  174.  
  175. private void generateSpline() {
  176. outlineLines.clear();
  177. outLinePoints.clear();
  178. splineLines.clear();
  179.  
  180. double step = 1. / w, t = step;
  181. DoublePoint[] points2 = new DoublePoint[n];
  182. int X, Y, Xold = (int) points[0].x, Yold = (int) points[0].y;
  183. for (int i = 0; i < n; i++) {
  184. X = (int) points[i].x;
  185. Y = (int) points[i].y;
  186. outLinePoints.add(new Point(X, Y));
  187. }
  188. if (n > 2) {
  189. int Xo = Xold, Yo = Yold;
  190. for (int i = 1; i < n; i++) {
  191. X = (int) points[i].x;
  192. Y = (int) points[i].y;
  193. if ((Xo < X) && (Yo < Y)) {
  194. if (random(1, 4)== 2) {
  195. X += random(0, 1);
  196. Y += random(1, 2);
  197. } else if (random(1, 4)== 2) {
  198. X += random(1, 2);
  199. Y += random(0, 1);
  200. }
  201. } else if ((Xo < X) && (Yo > Y)) {
  202. if (random(1, 4)== 2) {
  203. X += random(0, 1);
  204. Y -= random(1, 2);
  205. } else if (random(1, 4)== 2) {
  206. X += random(1, 2);
  207. Y -= random(0, 1);
  208. }
  209. } else if ((Xo > X) && (Yo < Y)) {
  210. if (random(1, 2)== 4) {
  211. X -= random(0, 1);
  212. Y += random(1, 2);
  213. } else if (random(1, 4)== 2) {
  214. X -= random(1, 2);
  215. Y += random(0, 1);
  216. }
  217. } else if ((Xo > X) && (Yo > Y)) {
  218. if (random(1, 2)== 2) {
  219. X -= random(0, 1);
  220. Y -= random(1, 2);
  221. } else if (random(1, 4)== 2) {
  222. X -= random(1, 2);
  223. Y -= random(0, 1);
  224. }
  225. }
  226. outlineLines.add(new Line(Xo, Yo, X, Y));
  227. Xo = X;
  228. Yo = Y;
  229. }
  230. }
  231. for (int k = 1; k < w; k++) {
  232. System.arraycopy(points, 0, points2, 0, n);
  233.  
  234. for (int j = n - 1; j > 0; j--) // points calculation
  235. for (int i = 0; i < j; i++) {
  236. points2[i].x = (1 - t) * points2[i].x + t * points2[i + 1].x;
  237. points2[i].y = (1 - t) * points2[i].y + t * points2[i + 1].y;
  238. }
  239.  
  240. X = (int) points2[0].x;
  241. Y = (int) points2[0].y;
  242. if ((Xold < X) && (Yold < Y)) {
  243. if (random(1, 4)== 2) {
  244. X += random(0, 1);
  245. Y += random(1, 2);
  246. } else if (random(1, 4)== 2) {
  247. X += random(1, 2);
  248. Y += random(0, 1);
  249. }
  250. } else if ((Xold < X) && (Yold > Y)) {
  251. if (random(1, 4)== 2) {
  252. X += random(0, 1);
  253. Y -= random(1, 2);
  254. } else if (random(1, 4)== 2) {
  255. X += random(1, 2);
  256. Y -= random(0, 1);
  257. }
  258. } else if ((Xold > X) && (Yold < Y)) {
  259. if (random(1, 2)== 4) {
  260. X -= random(0, 1);
  261. Y += random(1, 2);
  262. } else if (random(1, 4)== 2) {
  263. X -= random(1, 2);
  264. Y += random(0, 1);
  265. }
  266. } else if ((Xold > X) && (Yold > Y)) {
  267. if (random(1, 2)== 2) {
  268. X -= random(0, 1);
  269. Y -= random(1, 2);
  270. } else if (random(1, 4)== 2) {
  271. X -= random(1, 2);
  272. Y -= random(0, 1);
  273. }
  274. }
  275. splineLines.add(new Line(Xold, Yold, X, Y));
  276. Xold = X;
  277. Yold = Y;
  278. t += step;
  279. }
  280. paintSpline();
  281. }
  282.  
  283. public void setMousePos(int x, int y) {
  284. if (x < 0) {
  285. x = -1;
  286. }
  287. if (y < 0) {
  288. y = -1;
  289. }
  290. if (x > 765) {
  291. x = -1;
  292. }
  293. if (y > 504)
  294. y = -1;
  295. getClient().getMouse().dispatchEvent(new MouseEvent((Applet) getClient(), MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, x, y, 0, false, 0));
  296. }
  297.  
  298. private Canvas getCanvas() {
  299. if (canvas == null) {
  300. canvas = getClient().getCanvas();
  301. }
  302. return canvas;
  303. }
  304.  
  305. private void moveMouseInternal(int curX, int curY, int x, int y) {
  306. double distance = Point2D.distance(curX, curY, x, y);
  307. while (distance > 0) {
  308. if (curX < 0 || curY < 0) {
  309. curX = x;
  310. curY = y;
  311. }
  312. if (Math.round(curX) < Math.round(x))
  313. curX++;
  314. else if (Math.round(curX) > Math.round(x))
  315. curX--;
  316. if (Math.round(curY) < Math.round(y))
  317. curY++;
  318. else if (Math.round(curY) > Math.round(y))
  319. curY--;
  320. setMousePos(curX, curY);
  321. try {
  322. int secs = (int) speed;
  323. double nanos = speed - secs;
  324. int nanosReal = (int) (nanos * 1000);
  325. Thread.sleep(secs, nanosReal);
  326. } catch (InterruptedException e) {
  327. e.printStackTrace();
  328. }
  329. distance = Point2D.distance(curX, curY, x, y);
  330. }
  331. }
  332.  
  333. public void moveMouse(int x, int y, int randomX, int randomY) {
  334. Client client = getClient();
  335. int thisX = client.getMouseX(), thisY = client.getMouseY();
  336. if (thisX < 0 || thisY < 0) {
  337. switch (random(1, 5)) { // on which side of canvas should it enter
  338. case 1:
  339. thisX = 1;
  340. thisY = random(0, 500);
  341. setMousePos(thisX, thisY);
  342. break;
  343. case 2:
  344. thisX = random(0, 765);
  345. thisY = 501;
  346. setMousePos(thisX, thisY);
  347. break;
  348. case 3:
  349. thisX = 766;
  350. thisY = random(0, 500);
  351. setMousePos(thisX, thisY);
  352. break;
  353. case 4:
  354. thisX = random(0, 765);
  355. thisY = 1;
  356. setMousePos(thisX, thisY);
  357. break;
  358. }
  359. }
  360. if (thisX == x && thisY == y) {
  361. return;
  362. }
  363. if (Point2D.distanceSq(thisX, thisY, x, y) < 10) {
  364. splineLines.clear();
  365. splineLines.add(new Line(thisX, thisY, random(x, x + randomX), random(y, y + randomY)));
  366. paintSpline();
  367. } else {
  368. createSpline(new Point(thisX, thisY), new Point(random(x, x + randomX), random(y, y + randomY)));
  369. }
  370. splineMouse();
  371. }
  372.  
  373. public void moveMouse(int x, int y) {
  374. moveMouse(x, y, 0, 0);
  375. }
  376.  
  377. public void pressMouse(int x, int y, boolean button) {
  378. if (x < 0 || y < 0 || x > 756 || y > 503)
  379. return;
  380. try {
  381. getClient().getMouse().dispatchEvent(new MouseEvent((Applet) getClient(),
  382. MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), 0, x, y,
  383. 1, false, button ? MouseEvent.BUTTON1 : MouseEvent.BUTTON3));
  384. } catch (UiThreadingViolationException ignored) {
  385. }
  386. }
  387.  
  388. public void releaseMouse(int x, int y, boolean button) {
  389. if (x < 0 || y < 0 || x > 756 || y > 503)
  390. return;
  391. try {
  392. Client client = getClient();
  393. getClient().getMouse().dispatchEvent(new MouseEvent((Applet) client,
  394. MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), 0, x, y,
  395. 1, false, button ? MouseEvent.BUTTON1 : MouseEvent.BUTTON3));
  396.  
  397. getClient().getMouse().dispatchEvent(new MouseEvent((Applet) client,
  398. MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), 0, x,
  399. y, 1, false, button ? MouseEvent.BUTTON1
  400. : MouseEvent.BUTTON3));
  401. } catch (UiThreadingViolationException ignored) {
  402. }
  403. }
  404.  
  405. /**
  406. * Moves the mouse with randomness and clicks.
  407. *
  408. * @param p
  409. * @param randomX
  410. * @param randomY
  411. * @param button
  412. */
  413. public void clickMouse(Point p, int randomX, int randomY, boolean button) {
  414. moveMouse(p, randomX, randomY);
  415. botEnv.methods.sleep(random(50, 150));
  416. clickMouse(button);
  417. }
  418.  
  419. /**
  420. * Moves the mouse and clicks at the given position.
  421. *
  422. * @param p
  423. * @param button
  424. */
  425. public void clickMouse(Point p, boolean button) {
  426. clickMouse(p, 0, 0, button);
  427. }
  428.  
  429. /**
  430. * Moves the mouse with randomness
  431. *
  432. * @param p
  433. * @param randomX
  434. * @param randomY
  435. */
  436. public void moveMouse(Point p, int randomX, int randomY) {
  437. moveMouse(p.x, p.y, randomX, randomY);
  438. }
  439.  
  440. private void clickMouse(int x, int y, boolean button) {
  441. pressMouse(x, y, button);
  442. botEnv.methods.sleep(random(0, 70));
  443. releaseMouse(x, y, button);
  444. }
  445.  
  446. public void clickMouse(boolean button) {
  447. Client client = getClient();
  448. clickMouse(client.getMouseX(), client.getMouseY(), button);
  449. }
  450.  
  451. public Client getClient() {
  452. return botEnv.botInstance.getClient();
  453. }
  454.  
  455. public void onRepaint(Graphics g) {
  456. if (BotConstants.paintMousePaths)
  457. g.drawImage(buffImage, 0, 0, null);
  458. }
  459.  
  460. public void setMouseSpeed(double speed) {
  461. botEnv.mouseSpeed = speed;
  462. }
  463.  
  464. private class DoublePoint {
  465. public double x;
  466. public double y;
  467.  
  468. public DoublePoint(double x, double y) {
  469. this.x = x;
  470. this.y = y;
  471. }
  472. }
  473.  
  474. public class Line {
  475. public final int x1;
  476. public final int y1;
  477. public final int x2;
  478. public final int y2;
  479.  
  480. public Line(int x1, int y1, int x2, int y2) {
  481. this.x1 = x1;
  482. this.y1 = y1;
  483. this.x2 = x2;
  484. this.y2 = y2;
  485. }
  486. }
  487.  
  488. public void notifyOnClose() {
  489. botEnv.botInstance.getEventHandler().removePaintListener(this);
  490. }
  491.  
  492. public void dragMouse(Point destination, int randomX, int randomY) {
  493. Client client = getClient();
  494. int thisX = client.getMouseX(), thisY = client.getMouseY();
  495. pressMouse(thisX, thisY, true);
  496. botEnv.methods.sleep(10, 50);
  497. moveMouse(destination, randomX, randomY);
  498. thisX = client.getMouseX();
  499. thisY = client.getMouseY();
  500. botEnv.methods.sleep(10, 50);
  501. clickMouse(thisX, thisY, true);
  502. }
  503.  
  504. public void dragMouse(Point destination){
  505. dragMouse(destination, 0, 0);
  506. }
  507. }
Add Comment
Please, Sign In to add comment