2

Me and my friend made a game and we are trying to create a bot for it.

Its repeating the same action over and over again. But the mouse always follows the same path from point A to point B.

Is there a way to make the mouse move from point A to point B and never make it use the same path?

This is the code for the bot:

// import the robotjs library
var robot = require('robotjs');

function main() {
    console.log("Starting...");
    sleep(4000);

    // basic infinite loop
    while (true) {
        robot.moveMouse(764, 557);
        robot.mouseClick();
        robot.moveMouse(356, 432);
        sleep(8000);
    }
}

function sleep(ms) {
    Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}

main();

Im new with coding so please have some mercy ;)

Syscall
  • 19,327
  • 10
  • 37
  • 52
Rick
  • 21
  • 1
  • 1
    Well, if you aim that same path should not be repeated then there can be `n` number of permutations possible. You need to have a list of some defined path and can try to switch between those randomly. – Diwakar Singh Mar 02 '21 at 10:09
  • Isn't there a option to randomize the movement of the mouse between point A and B? So the mouse will most likely never use the same path twice? – Rick Mar 02 '21 at 10:21
  • @Rick you are missing the point here... "likely never" is not the case - it will "surely at some point" use the same path as before. And if the script will be fast enough, it will be really soon. – Flash Thunder Mar 16 '21 at 19:29

1 Answers1

0

Here you go:

const robot = require("robotjs")

const START = [764, 557]
const END = [356, 432]
const loops = 100

robot.setMouseDelay(0)

const getRandom = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

const go = (coord) => {
    return new Promise((resolve) => {
        robot.moveMouseSmooth(getRandom(END[0], START[0]), getRandom(END[1], START[1]), 1)
        robot.moveMouseSmooth(coord[0], coord[1], 1)
        resolve()
    })
}

const recurse = async (loops) => {
    for (let i = 0; i < loops; i++) {
        await go(START)
        console.log("click") // Insert clicking action here, for single left mouseclick just use, robot.mouseClick() 
        await go(END)
    }
} 

recurse(loops)