Fixed a bug with InputController.moveMouse

This commit is contained in:
dtookey 2023-08-07 07:27:10 -04:00
parent 08a27661b9
commit 6b082a383a
7 changed files with 50 additions and 97 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -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:

View File

@ -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)
}
}

View File

@ -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)}")
}
}

View File

@ -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
)

View File

@ -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
}
}
}