Base Zones

credits to cazka for basically 99% of this script, no this is not a multiboxing script

As of 2021-10-10. See the latest version.

// ==UserScript==
// @name         Base Zones
// @description  credits to cazka for basically 99% of this script, no this is not a multiboxing script
// @version      15.7.6
// @author       none
// @match        *://diep.io/*
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addValueChangeListener
// @grant        GM_removeValueChangeListener
// @namespace https://greatest.deepsurf.us/users/790354
// ==/UserScript==
'use strict';


class Gui {
    constructor(title) {
        this._buttons = [];
        this._notifications = [];

        this._title = title;
        this._gui;
        this._guiHead;
        this._guiBody;
        this._notificationBody;
        this._notificationColor = 0;

        this._init();
        this._enableShortcuts();
    }
    _init() {
        const nonce = `a${Math.floor(Math.random() * 1e5)}`;
        GM_addStyle(
            `.${nonce} button{display:none;font-family:Ubuntu;color:#fff;text-shadow:-.1em -.1em 0 #000,0 -.1em 0 #000,.1em -.1em 0 #000,.1em 0 0 #000,.1em .1em 0 #000,0 .1em 0 #000,-.1em .1em 0 #000,-.1em 0 0 #000;opacity:.8;border:0;padding:.3em .5em;width:100%;transition:all .15s}.${nonce}{top:0;left:0;position:absolute}.${nonce} button:active:not([disabled]){filter:brightness(.9)}.${nonce} button:hover:not([disabled]):not(:active){filter:brightness(1.1)}`
        );
    }

    addButton(text, onclick, keyCode) {
        return this._addButton(this._guiBody, text, onclick, keyCode);
    }

    notification(text, duration = 1) {
        const button = document.createElement('button');
        button.innerText = text;
        button.style['background-color'] = this._colors[this._notificationColor++ % this._colors.length];
        button.style.display = 'block';
        button.addEventListener('contextmenu', (e) => e.preventDefault());

        this._notificationBody.appendChild(button);
        setTimeout(() => button.remove(), duration);
    }

    _addButton(parent, text, onclick, keyCode) {
        const button = document.createElement('button');
        button.innerHTML = text;
        button.keyCode = keyCode;
        button.onclick = onclick;
        button.style['background-color'] = this._colors[this._buttons.length % this._colors.length];
        button.addEventListener('contextmenu', (e) => e.preventDefault());

        parent.appendChild(button);
        this._buttons.push(button);
        return button;
    }

    _enableShortcuts() {
        unsafeWindow.onkeydown = new Proxy(unsafeWindow.onkeydown, {
            apply: (target, thisArgs, args) => {
                if (document.getElementById('textInputContainer').style.display === 'block') {
                    return Reflect.apply(target, thisArgs, args);
                }
                this._buttons.forEach((button) => {
                    if (button.keyCode === event.code) button.onclick();
                });
                return Reflect.apply(target, thisArgs, args);
            },
        });
    }
}
class Minimap {
    constructor() {
        this._minimapWidth;
        this._minimapHeight;
        this._x00;
        this._y00;
        this._pointX;
        this._pointY;
        this._pointX_previous;
        this._pointY_previous;
        this._viewportWidth;
        this._viewportHeight;
        this._fov;

        this._minimapHook();
        this._arrowHook();
        this._viewportHook();
        this._fovHook();
    }
    get x() {
        return this._pointX ? (this._pointX - this._x00) / this._minimapWidth : 0;
    }
    get y() {
        return this._pointY ? (this._pointY - this._y00) / this._minimapHeight : 0;
    }
    get x_previous() {
        return this._pointX_previous ? (this._pointX_previous - this._x00) / this._minimapWidth : 0;
    }
    get y_previous() {
        return this._pointY_previous ? (this._pointY_previous - this._y00) / this._minimapHeight : 0;
    }
    get scale() {
        return {
            x: this._viewportWidth / this._minimapWidth,
            y: this._viewportHeight / this._minimapHeight,
        };
    }
    get fov() {
        return this._fov;
    }

    _minimapHook() {
        let setTransformArgs;

        const onsetTransform = (args) => {
            if (args[0] === args[3]) setTransformArgs = args;
        };
        const onstrokeRect = () => {
            if (setTransformArgs) {
                this._minimapWidth = setTransformArgs[0];
                this._minimapHeight = setTransformArgs[3];
                this._x00 = setTransformArgs[4];
                this._y00 = setTransformArgs[5];
                setTransformArgs = undefined;
            }
        };
        this._ctxHook('setTransform', onsetTransform);
        this._ctxHook('strokeRect', onstrokeRect);
    }
    _arrowHook() {
        let index = 0;
        const stack = Array(4);

        let pointA;
        let pointB;
        let pointC;

        const calculatePos = () => {
            const side1 = Math.floor(
                Math.sqrt(Math.pow(pointA[0] - pointB[0], 2) + Math.pow(pointA[1] - pointB[1], 2))
            );
            const side2 = Math.floor(
                Math.sqrt(Math.pow(pointA[0] - pointC[0], 2) + Math.pow(pointA[1] - pointC[1], 2))
            );
            const side3 = Math.floor(
                Math.sqrt(Math.pow(pointB[0] - pointC[0], 2) + Math.pow(pointB[1] - pointC[1], 2))
            );
            if (side1 == side2 && side2 == side3) return;

            this._pointX_previous = this._pointX;
            this._pointY_previous = this._pointY;

            this._pointX = (pointA[0] + pointB[0] + pointC[0]) / 3;
            this._pointY = (pointA[1] + pointB[1] + pointC[1]) / 3;
        };
        const onbeginPath = () => {
            index = 0;
            stack[index++] = 0;
        };
        const onmoveTo = (args) => {
            if (index === 1 && stack[index - 1] === 0) {
                stack[index++] = 1;
                pointA = args;
                return;
            }
            index = 0;
        };
        const onlineTo = (args) => {
            if (index === 2 && stack[index - 1] === 1) {
                stack[index++] = 2;
                pointB = args;
                return;
            }
            if (index === 3 && stack[index - 1] === 2) {
                stack[index++] = 2;
                pointC = args;
                return;
            }
            index = 0;
        };
        const onfill = () => {
            if (index === 4 && stack[index - 1] === 2) {
                calculatePos();
                return;
            }
            index = 0;
        };

        this._ctxHook('beginPath', onbeginPath);
        this._ctxHook('moveTo', onmoveTo);
        this._ctxHook('lineTo', onlineTo);
        this._ctxHook('fill', onfill);
    }
    _viewportHook() {
        let setTransformArgs;

        const onsetTransform = (args) => {
            if ((args[0] / args[3]).toFixed(4) !== (unsafeWindow.innerWidth / unsafeWindow.innerHeight).toFixed(4))
                return;
            if (args[0] >= unsafeWindow.innerWidth && args[3] >= unsafeWindow.innerHeight) return;

            setTransformArgs = args;
        };
        const onfillRect = () => {
            if (setTransformArgs) {
                unsafeWindow.input.set_convar('ren_minimap_viewport', false);
                this._viewportWidth = setTransformArgs[0];
                this._viewportHeight = setTransformArgs[3];
                setTransformArgs = undefined;
            }
        };

        this._ctxHook('setTransform', onsetTransform);
        this._ctxHook('fillRect', onfillRect);

        setTimeout(() => unsafeWindow.input.set_convar('ren_minimap_viewport', true), 1000);
        setInterval(() => {
            unsafeWindow.input.set_convar('ren_minimap_viewport', true);
        }, 1000);
    }
    _fovHook() {
        let solid_background = false;
        setTimeout(() => {
            solid_background = unsafeWindow.input.get_convar('ren_solid_background') === 'true' ? true : false;
        }, 1000);

        const calculateFov = (fov) => {
            this._fov = fov * 10;
        };
        function onstroke() {
            if (this.fillStyle === '#cdcdcd') {
                if (solid_background) unsafeWindow.input.set_convar('ren_solid_background', true);
                calculateFov(this.globalAlpha);
            }
        }

        this._ctxHook('stroke', onstroke);

        setInterval(() => {
            if (solid_background) unsafeWindow.input.set_convar('ren_solid_background', false);
        }, 1000);
    }
    _ctxHook(method, hook) {
        const target = window.CanvasRenderingContext2D.prototype;
        target[method] = new Proxy(target[method], {
            apply(target, thisArg, args) {
                args = hook.call(thisArg, args) || args;
                return target.apply(thisArg, args);
            },
        });
    }
}
class DiepGamepad {
    constructor() {
        this._axes = [0, 0, 0, 0];
        this._buttons = [...Array(17)].map((x) => {
            return { pressed: false };
        });
    }

    set x(value) {
        this._axes[0] = value;
    }
    set y(value) {
        this._axes[1] = value;
    }
    set mx(value) {
        this._axes[2] = value;
    }
    set my(value) {
        this._axes[3] = value;
    }
    set leftMouse(value) {
        this._buttons[7].pressed = value;
    }
    set rightMouse(value) {
        this._buttons[6].pressed = value;
    }
    set connected(value) {
        unsafeWindow.navigator.getGamepads = () => [value ? this.toGamepad() : undefined];
    }

    get x() {
        return this._axes[0];
    }
    get y() {
        return this._axes[1];
    }
    get mx() {
        return this._axes[2];
    }
    get my() {
        return this._axes[3];
    }
    get leftMouse() {
        return this._buttons[7].pressed;
    }
    get rightMouse() {
        return this._buttons[6].pressed;
    }
    get connected() {
        return unsafeWindow.navigator.getGamepads()[0] ? true : false;
    }

    toGamepad() {
        return {
            axes: this._axes,
            buttons: this._buttons,
            mapping: 'standard',
        };
    }
}
class Vector {

}
class Arena {
    static get BLOCKSIZE() {
        return 50;
    }
    static scale(x, y) {
        return {
            x: Math.floor(22300 * (x - 0.5) + 0.5),
            y: Math.floor(22300 * (y - 0.5) + 0.5),
        };
    }
    static unscale(x, y) {
        return {
            x: x / 22300 + 0.5,
            y: y / 22300 + 0.5,
        };
    }
}
class Player {
    constructor() {
        this._minimap = new Minimap();
        this._gamepad = new DiepGamepad();

        this._mouse = {
            x: 0,
            y: 0,
        };
        this._inputs = {
            left: false,
            down: false,
            up: false,
            right: false,
        };
        this._dead = true;

        unsafeWindow.addEventListener('mousemove', (e) => this._onmousemove(e));
        unsafeWindow.addEventListener('mousedown', (e) => this._onmousedown(e));
        unsafeWindow.addEventListener('mouseup', (e) => this._onmouseup(e));
        unsafeWindow.addEventListener('keydown', (e) => this._onkeydown(e));
        unsafeWindow.addEventListener('keyup', (e) => this._onkeyup(e));
        //Dead Listener
        new MutationObserver((args) => {
            this._dead = args[0].target.style.display === 'block';
            if (this.ondead && this._dead) this.ondead();
        }).observe(document.getElementById('a'), { attributes: true });
        //Message Listener
        const notify = (text) => {
            if (this.onmessage) this.onmessage(text);
        };
        CanvasRenderingContext2D.prototype.fillText = new Proxy(CanvasRenderingContext2D.prototype.fillText, {
            apply(target, thisArg, args) {
                if (args[0].startsWith("You've killed ")) {
                    notify(args[0]);
                }
                return Reflect.apply(target, thisArg, args);
            },
        });
    }

    set useGamepad(value) {
        this._gamepad.connected = value;
    }

    get position() {
        const position = Arena.scale(this._minimap.x, this._minimap.y);
        const previous = Arena.scale(this._minimap.x_previous, this._minimap.y_previous);
        return {
            x: position.x,
            y: position.y,
            x_previous: previous.x,
            y_previous: previous.y,
        };
    }
    get mouse() {
        return this.toArenaPos(this._mouse.x, this._mouse.y);
    }
    get inputs() {
        return this._inputs;
    }
    get dead() {
        return this._dead;
    }

    get gamemode() {
        return unsafeWindow.localStorage.gamemode;
    }

    keyDown(key) {
        unsafeWindow.input.keyDown(key);
        this._onkeydown({ keyCode: key });
    }
    keyUp(key) {
        unsafeWindow.input.keyUp(key);
        this._onkeyup({ keyCode: key });
    }
    spawn(name) {
        if (!this.dead) return;

        if (name !== undefined) {
            document.getElementById('textInput').value = name;
        }

        this.keyDown(13);
        this.keyUp(13);

        setTimeout(() => {
            if (document.getElementById('textInputContainer').style.display === 'none') return;
            this.keyDown(13);
            this.keyUp(13);
        }, 300);
    }
    toScreenPos(x, y) {
        const unscale = Arena.unscale(x, y);
        x = unscale.x;
        y = unscale.y;

        let position = this.position;
        position = Arena.unscale(position.x, position.y);
        const scale = this._minimap.scale;

        const a = window.innerHeight / 1080;
        const b = window.innerWidth / 1920;
        const c = b < a ? a : b;
        const unknown = this._minimap.fov * c;

        x -= position.x;
        x /= scale.x;
        x += 0.5;
        x *= unsafeWindow.innerWidth;

        y -= position.y;
        y /= scale.y;
        y += 0.5;
        y *= unsafeWindow.innerHeight;

        return { x, y };
    }

}
class AAAAAA {

}
/**
 *this code is useless, but do not delete as that messes up something
 */
class Chat {
    constructor(player) {
        this._player = player;
        this._inputBox = document.body.appendChild(document.createElement('div'));
        this._hookonkeydown();
        this._hookAnimationFrame();
    }
    _onkeydown(e) {
        let preventDefault = false;

        if (player.dead) return;

        return preventDefault;
    }
    _hookonkeydown() {
        const _this = this;
        unsafeWindow.onkeydown = new Proxy(unsafeWindow.onkeydown, {
        });
    }
    _hookAnimationFrame() {
        const _this = this;
        unsafeWindow.requestAnimationFrame = new Proxy(unsafeWindow.requestAnimationFrame, {
        });
    }
}



function drawZones() {
    if (player.dead) return;

    ctx.save();

    ctx.globalAlpha = 0.08;

    if (player.gamemode === '4teams') {

        //blue
        ctx.fillStyle = '#006480';
        ctx.beginPath();
        ctx.fillRect(1729, 888, 40, 40);

        //purple
        ctx.fillStyle = '#644280';
        ctx.beginPath();
        ctx.fillRect(1857, 888, 40, 40);

        //green
        ctx.fillStyle = '#00803e';
        ctx.beginPath();
        ctx.fillRect(1729, 1016, 40, 40);

        //red
        ctx.fillStyle = '#963033';
        ctx.beginPath();
        ctx.fillRect(1857, 1016, 40, 40,);
    } else if (player.gamemode === 'teams') {

        //blue
        ctx.fillStyle = '#006480';
        ctx.fillRect(1729, 888, 40, 170);

        //red
        ctx.fillStyle = '#963033';
        ctx.fillRect(1857, 888, 40, 170);
    }
    ctx.restore();
}

const player = new Player();
const storage = new AAAAAA();
const chat = new Chat(player);

unsafeWindow.addEventListener('unload', () => {
    if (player.isMaster) {
        storage.reset();
    }
});

player.onkeyDown = (key) => {
    if (player.isMaster && !chat._chatmode) {
        storage.keyDown = key;
        storage.keyDown = -1;
    }
};
player.onkeyUp = (key) => {
    if (player.isMaster) {
        storage.keyUp = key;
        storage.keyUp = -1;
    }
};

player.onmessage = (text) => {
    if (!player.isMaster) {
        storage.notification = text;
    }
};

unsafeWindow.addEventListener('keydown', (e) => {
    if (!player.isMaster && e.keyCode == 13) {
        const input = document.getElementById('textInput');
        input.value = input.value.startsWith('\u0044\u0042') ? input.value : '\u0044\u0042 ' + input.value;
    }
});

//setup canvas
const ctx = document.getElementById('canvas').getContext('2d');

// run main Loop
unsafeWindow.requestAnimationFrame = new Proxy(unsafeWindow.requestAnimationFrame, {
    apply: function (target, thisArg, args) {
        drawZones();
        if (player.isMaster) return Reflect.apply(target, thisArg, args);
        else setTimeout(() => Reflect.apply(target, thisArg, args), 0);
    },
});