Memory based AFK

set Afk spot with Q, toggle afk with R

// ==UserScript==
// @name         Memory based AFK
// @namespace    http://tampermonkey.net/
// @version      0.0.1
// @description  set Afk spot with Q, toggle afk with R
// @author       r!PsAw
// @match        https://diep.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=diep.io
// @grant        unsafeWindow
// @run-at       document-start
// @license      MIT
// ==/UserScript==

//define win
const win = typeof unsafeWindow !== "undefined" ? unsafeWindow : window;

//keys bypass
(function() {
    win.frozenHasFocus = {
        hasFocus: () => true
    };
    document.hasFocus = () => true;
})();

//memory hook
win.exists = false;
win.Object.defineProperty(win.Object.prototype, "HEAPF32", {
    get: function() {
        return undefined;
    },
    set: function(newHeapF32) {
        if (!newHeapF32 || !this.HEAPU32) return;
        delete win.Object.prototype.HEAPF32;
        window.Module = this;
        window.Module.HEAPF32 = newHeapF32;
        win.Module = window.Module;
        win.exists = true;
    },
    configurable: true,
    enumerable: true
});

//keys definition
const diep_keys = [
  "KeyA", "KeyB", "KeyC", "KeyD", "KeyE", "KeyF", "KeyG", "KeyH", "KeyI", "KeyJ", "KeyK", "KeyL", "KeyM", "KeyN", "KeyO", "KeyP", "KeyQ", "KeyR", "KeyS", "KeyT", "KeyU", "KeyV", "KeyW", "KeyX", "KeyY", "KeyZ",
  "ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight", "Tab", "Enter", "NumpadEnter", "ShiftLeft", "ShiftRight", "Space", "Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9",
  "Digit0", "Digit1", "Digit2", "Digit3", "Digit4", "Digit5", "Digit6", "Digit7", "Digit8", "Digit9", "F2", "End", "Home", "Semicolon", "Comma", "NumpadComma", "Period", "Backslash"
].reduce((n, e, c) => {
    n[e] = c + 1;
    return n;
}, {});

function key_down(Key){
    input.onKeyDown(diep_keys[Key]);
    console.log('pressing', Key);
}

function key_up(Key){
    input.onKeyUp(diep_keys[Key]);
    console.log('unpressing', Key);
}

//AFK logic
let afk = false;
let moving = false;
let your_pos = {x: 0, y: 0};
let goal = {x: 0, y: 0};

document.onkeydown = function(e) {
    //console.log(e.key);
    if(e.key === "q" || e.key === "Q"){
        input.inGameNotification(`New position selected at x: ${Math.floor(your_pos.x)} y: ${Math.floor(your_pos.y)}`, 9999);
        set_goal(your_pos.x, your_pos.y);
    }else if(e.key === "r" || e.key === "R"){
        afk = !afk;
        let message = afk? "ON" : "OFF";
        input.inGameNotification(`AFK: ${message}`, 999);
    }
};

function get_your_pos(){
    window.requestAnimationFrame(get_your_pos);
    if(win.exists){
        your_pos.x = Module.HEAPF32[18888];
        your_pos.y = Module.HEAPF32[18889];
    }
}
window.requestAnimationFrame(get_your_pos);

function set_goal(x, y){
    console.log("set_goal");
    goal.x = x;
    goal.y = y;
}

function move_to_goal() {
    console.log(`YOU: x: ${your_pos.x} y: ${your_pos.y} GOAL x: ${goal.x} y: ${goal.y}`);
    if (afk) {
        if (your_pos.x > goal.x) {
            key_up("KeyD");
            key_down("KeyA");
        } else {
            key_up("KeyA");
            key_down("KeyD");
        }
        if (your_pos.y > goal.y) {
            key_up("KeyS");
            key_down("KeyW");
        } else {
            key_up("KeyW");
            key_down("KeyS");
        }
        moving = true;
    }else{
        if(moving){
          key_up("KeyW");
          key_up("KeyA");
          key_up("KeyS");
          key_up("KeyD");
          moving = false;
        }
    }
}
setInterval(move_to_goal, 100);