From 6b082a383aeb2f703a992b229ede32f2bb1cda4f Mon Sep 17 00:00:00 2001 From: dtookey Date: Mon, 7 Aug 2023 07:27:10 -0400 Subject: [PATCH] Fixed a bug with InputController.moveMouse --- .idea/gradle.xml | 1 + .idea/misc.xml | 2 +- src/main/kotlin/Controllers.kt | 54 +++++++++----- src/main/kotlin/Entry.kt | 12 +++- src/main/kotlin/Orchestrator.kt | 2 +- src/main/kotlin/RSLogic.kt | 4 +- .../kotlin/controllers/InputControllerTest.kt | 72 ------------------- 7 files changed, 50 insertions(+), 97 deletions(-) delete mode 100644 src/test/kotlin/controllers/InputControllerTest.kt diff --git a/.idea/gradle.xml b/.idea/gradle.xml index f9163b4..ce1c62c 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ + \ No newline at end of file diff --git a/src/main/kotlin/Controllers.kt b/src/main/kotlin/Controllers.kt index 5dc12f3..b581bd2 100644 --- a/src/main/kotlin/Controllers.kt +++ b/src/main/kotlin/Controllers.kt @@ -169,7 +169,7 @@ interface DesktopController { } else { -1 } - return Point(point.x + (xDel * xDir), point.y + (yDel + yDir)) + return Point(point.x + (xDel * xDir), point.y + (yDel * yDir)) } } @@ -233,31 +233,49 @@ interface Automaton : DesktopController, InputController, TemporalController * @param robot The Robot instance to use. A default is created if not provided. */ open class RobotController(private val robot: Robot = Robot()) : Automaton { + /** - * Moves the mouse cursor to the given [Point] destination. + * Moves the mouse pointer to the given [Point] coordinates. * - * Uses the Robot [mouseMove] method to move the mouse cursor to the x and y - * coordinates specified by the provided [Point]. + * This will use the [Robot] API to move the mouse to the x,y position. + * It also does validation to retry the move if ended up at incorrect location. * - * Adding some random variance to the target [Point] can simulate human-like - * imperfect mouse movements. - * - * Example usage: + * Usage examples: * * ``` - * val robot = RobotController() - * - * // Target destination point - * val target = Point(100, 200) - * - * // Move mouse to target - * robot.mouseMove(target) + * val controller = DesktopController() + * controller.moveMouse(Point(100, 200)) * ``` * - * @param destination The [Point] representing the target x and y coordinates. + * + * ``` + * val target = Point(500, 300) + * controller.moveMouse(target) + * ``` + * + * @param point The destination [Point] x,y coordinates to move the mouse to. + * + * + + * */ override fun moveMouse(point: Point) { robot.mouseMove(point.x, point.y) + + //There's a bug in OpenJDK that results in incorrect cursor position in the [Robot.mouseMove] function if using + //a Windows Resolution scale other than 100%. As a result, we have to check that the cursor made it all the way. + //From some anecdotal observation, it has an overshoot/decay pattern sort of like a binary search. The mouse will + //usually be in the correct place within 2-3 loop itterations + + repeat(10) { + val rPoint = getPointerLocation() + //here, we check the points and if we're good, we + if (rPoint.x == point.x && rPoint.y == point.y) { + return + } else { + robot.mouseMove(point.x, point.y) + } + } } /** @@ -332,7 +350,7 @@ open class RobotController(private val robot: Robot = Robot()) : Automaton { /** * Scrolls the mouse wheel down by one unit. * - * Uses the Robot [mouseWheel] method to scroll down and then sleeps + * Uses the [Robot.mouseWheel] method to scroll down and then sleeps * for a random duration between 10-20ms to pace the scrolling. * * Example usage: @@ -354,7 +372,7 @@ open class RobotController(private val robot: Robot = Robot()) : Automaton { /** * Scrolls the mouse wheel up by one unit. * - * Uses the Robot [mouseWheel] method to scroll up and then sleeps for a + * Uses the [Robot.mouseWheel] method to scroll up and then sleeps for a * random duration between 10-20ms to pace the scrolling. * * Example usage: diff --git a/src/main/kotlin/Entry.kt b/src/main/kotlin/Entry.kt index cbdf419..6b0770f 100644 --- a/src/main/kotlin/Entry.kt +++ b/src/main/kotlin/Entry.kt @@ -1,8 +1,14 @@ import java.awt.Point fun main() { - println("Hello") -// Routines.fullRunIncense(0, 0, 0, 257) - Routines.processInventoryAtFurnace(566) + val agent = RSOrchestrator.getInstance() + val p = agent.getBankPoint() + repeat(25){ +// val newPoint = agent.automaton.getAlmostPoint(p, WiggleParams(10, 10)) + val newPoint = p + println("Moving mouse to $newPoint") + agent.automaton.moveMouse(newPoint) + agent.automaton.sleep(500) + } } diff --git a/src/main/kotlin/Orchestrator.kt b/src/main/kotlin/Orchestrator.kt index 09a5ded..23f1c32 100644 --- a/src/main/kotlin/Orchestrator.kt +++ b/src/main/kotlin/Orchestrator.kt @@ -247,7 +247,7 @@ interface Orchestrator { } print("\r ") val finish = System.currentTimeMillis() - println("Finished everything in ${HelperFunctions.prettyTimeString(finish - start)}") + println("\rFinished everything in ${HelperFunctions.prettyTimeString(finish - start)}") } } diff --git a/src/main/kotlin/RSLogic.kt b/src/main/kotlin/RSLogic.kt index 5fbc1bd..e3e1cec 100644 --- a/src/main/kotlin/RSLogic.kt +++ b/src/main/kotlin/RSLogic.kt @@ -237,7 +237,7 @@ private class RSAgent(override val automaton: Automaton = RobotController()) : R taskParams: StandingTaskParams ) { //open the bank located by the chest parameter - moveMouseLeftClickAndSleep(automaton.getAlmostPoint(taskParams.bankPoint, WiggleParams()), 900, 400) + moveMouseLeftClickAndSleep(automaton.getAlmostPoint(taskParams.bankPoint, WiggleParams(10, 10)), 900, 400) //withdraw the desired inventory preset automaton.keyPress(taskParams.bankPresetHotkey) //sleep for a server tick @@ -282,7 +282,7 @@ private class RSAgent(override val automaton: Automaton = RobotController()) : R ) { //move to the bank and open the interface moveMouseLeftClickAndSleep( - automaton.getAlmostPoint(taskParams.bankPoint, WiggleParams()), + automaton.getAlmostPoint(taskParams.bankPoint, WiggleParams(10, 10)), taskParams.travelDurationMillis, taskParams.travelDurationVarianceMillis ) diff --git a/src/test/kotlin/controllers/InputControllerTest.kt b/src/test/kotlin/controllers/InputControllerTest.kt deleted file mode 100644 index e624571..0000000 --- a/src/test/kotlin/controllers/InputControllerTest.kt +++ /dev/null @@ -1,72 +0,0 @@ -package controllers -import org.junit.Test -import org.junit.Assert.* -import java.awt.Point - -class InputControllerTest { - - private val controller = TestInputController() - - @Test - fun testMoveMouse() { - val point = Point(100, 200) - controller.moveMouse(point) - assertEquals(point, controller.lastMovedPoint) - } - - @Test - fun testMouseClick() { - controller.mouseClick(InputEvent.BUTTON1_MASK) - assertTrue(controller.button1Clicked) - } - - @Test - fun testKeyPress() { - val keyCode = KeyEvent.VK_A - controller.keyPress(keyCode) - assertEquals(keyCode, controller.lastPressedKey) - } - - @Test - fun testScrollIn() { - controller.scrollIn(10, 5) - assertTrue(controller.didScrollIn) - } - - @Test - fun testScrollOut() { - controller.scrollOut(10, 5) - assertTrue(controller.didScrollOut) - } - - private class TestInputController : InputController { - - var lastMovedPoint: Point? = null - var button1Clicked = false - var lastPressedKey: Int? = null - var didScrollIn = false - var didScrollOut = false - - override fun moveMouse(point: Point) { - lastMovedPoint = point - } - - override fun mouseClick(button: Int) { - if (button == InputEvent.BUTTON1_MASK) { - button1Clicked = true - } - } - - override fun keyPress(keyCode: Int) { - lastPressedKey = keyCode - } - - override fun scrollIn(sleepDur: Long, sleepDurVariance: Long) { - didScrollIn = true - } - - override fun scrollOut(sleepDur: Long, sleepDurVariance: Long) { - didScrollOut = true - } - } -} \ No newline at end of file