Diep.io Auto-leveler Bot

Press Q to toggle, must be in base at least once in a lifetime so the script knows what team you are in

// ==UserScript==
// @name         Diep.io Auto-leveler Bot
// @namespace    http://tampermonkey.net/
// @version      2024-03-24 - 2024-04-14
// @description  Press Q to toggle, must be in base at least once in a lifetime so the script knows what team you are in
// @author       Mi300
// @match        https://diep.io/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @license      Apache License 2.0
// @grant        none
// @run-at      document-start
// ==/UserScript==



/*                                             --------HOW TO USE--------
//              1. Use in 2tdm or 4tdm. in any other gamemode, it breaks!
//              2. Press Q to toggle on / off
//              3. Player must be in their base at least once so the script knows what team they are in
//              4. If you get disconnected by the anti-cheat just refresh the page it should fix it
//              5. The script is only functional if you are on the tab or if the tab is a seperate window
//              6. If you see any issues tell me on discord (Mi300), but I probably won't do anything about it
//
//                                             --------------------------
*/













const ARENA_WIDTH = 26000;
const ARENA_HEIGHT = 26000;

const T4_BASE_WIDTH = 3900;
const T4_BASE_HEIGHT = 3900;

const T2_BASES = [
  {
    id: 0,
    name: "blue",
    hex: "#00b2e1",
    x: 0,
    y: 0,
    cX: 0,
    cY: 0,
    dirX: 1,
    dirY: 1,
  },
  {
    id: 3,
    name: "red",
    hex: "#f14e54",
    x: 23500,
    y: 0,
    cX: ARENA_WIDTH,
    cY: ARENA_HEIGHT,
    dirX: -1,
    dirY: 1,
  },
]

const T4_BASES = [
  {
    id: 0,
    name: "blue",
    hex: "#00b2e1",
    x: 0,
    y: 0,
    cX: 0,
    cY: 0,
    dirX: 1,
    dirY: 1,
  },
  {
    id: 1,
    name: "purple",
    hex: "#bf7ff5",
    x: 22100,
    y: 0,
    cX: 0,
    cY: ARENA_HEIGHT,
    dirX: -1,
    dirY: 1,
  },
  {
    id: 2,
    name: "green",
    hex: "#00e16e",
    x: 0,
    y: 22100,
    cX: ARENA_WIDTH,
    cY: 0,
    dirX: 1,
    dirY: -1,
  },
  {
    id: 3,
    name: "red",
    hex: "#f14e54",
    x: 22100,
    y: 22100,
    cX: ARENA_WIDTH,
    cY: ARENA_HEIGHT,
    dirX: -1,
    dirY: -1,
  },
]

alert("Auto Leveler: Press Q to toggle on / off.")

let OMG = setInterval(function(){
  if(!window.input){
    return;
  }
  clearInterval(OMG);

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let colors = {
  minimapArrow: '#000000',
  players: '#f14e54',
  squares: '#ffe869',
  triangles: '#fc7677',
  pentagons: '#768dfc',
  alphaPentagons: '#768dfc',
  bullets: '#f14e54',
  drones: '#f14e54',
  crashers: '#f177dd',
  necromancerDrone: '#fcc376',
}
let enemies = [];
let squares = [];
let tempsquares = [];
let triangles = [];
let temptriangles = [];
let pentagons = [];
let temppentagons = [];
let dead = true;
let baseArea;
let inBase = true;
let goal;
let toggled = false;
let lastCheck = Date.now();
let is2T = false;

let minimapArrow = [0, 0];
let minimapPos = [0, 0];
let minimapDim = [0, 0];
let playerPos = [0, 0];

document.addEventListener("keydown", function(e){
  if(e.key == "q"){
    toggled = !toggled;
    setTimeout(function(){
      input.key_up(87);
      input.key_up(83);
      input.key_up(65);
      input.key_up(68);
    },200);
  }
});

function getCentre(vertices) {
  let centre = [0, 0];
  vertices.forEach (vertex => {
    centre [0] += vertex[0]
    centre [1] += vertex[1]
  });
  centre[0] /= vertices.length;
  centre[1] /= vertices.length;
  return centre;
}
function getClosest(entities) {
  let acc = [[0, 0], 0]
  for (let i = 0; i < entities.length; i ++) {
    const accumulator = getDist (acc[0], [canvas.width / 2, canvas.height / 2])[0];
    const current = getDist (entities[i][0], [canvas.width / 2, canvas.height / 2])[0];

    if (current < accumulator) acc = entities[i];
  }
  return acc;
}
function invertCoordinate(coord){
  return [canvas.width - coord[0], canvas.height - coord[1]];
}
function getDist(t1, t2){
  const distX = t1[0] - t2[0];
  const distY = t1[1] - t2[1];

  return [Math.hypot(distX, distY), distX, distY];
};

function hook(target, callback){

  function check(){
    window.requestAnimationFrame(check)

    const func = CanvasRenderingContext2D.prototype[target]

    if(func.toString().includes(target)){

      CanvasRenderingContext2D.prototype[target] = new Proxy (func, {
        apply (method, thisArg, args) {
          callback(thisArg, args)

        return Reflect.apply (method, thisArg, args)
        }
      });
    }
  }
  window.requestAnimationFrame(check)
}

let calls = 0;
let points = [];
  hook('beginPath', function(thisArg, args){
    calls = 1;
    points = [];
  });
  hook('moveTo', function(thisArg, args){
    if (calls == 1) {
      calls+=1;
      points.push(args)
    } else {
      calls = 0;
    }
  });
  hook('lineTo', function(thisArg, args){
    if (calls >= 2 && calls <= 6) {
      calls+=1;
      points.push(args)
    } else {
      calls = 0;
    }
  });
  hook('fill', function(thisArg, args){
    if(thisArg.fillStyle == "#00e16e"){
      lastCheck = Date.now();
    }

    if(calls >= 4 && calls <= 6) {
      const centre = getCentre(points);
      const list = calls == 4 ? triangles : calls == 5 ? squares : pentagons;


      if(thisArg.globalAlpha < 1){
        return;
      }
      if(thisArg.fillStyle == "#000000"){
        if(true){
          minimapArrow = centre;
        }

        return;
      }
      if(!baseArea){
        return;
      }
      if((calls == 5 || calls == 4) && ["#00b2e1", "#bf7ff5", "#00e16e", "#f14e54"].includes(thisArg.fillStyle)){
          if(baseArea.hex == thisArg.fillStyle){
            return;
          }
          enemies.push([centre, 0, thisArg.fillStyle])
          return;
        }

      if (!['#ffe869', '#fc7677', '#768dfc'].includes(thisArg.fillStyle)) {
        let acc = [[0, 0], 0]
        for (let i = 0; i < list.length; i ++) {
          const accumulator = getDist (acc[0], centre)[0];
          const current = getDist (list[i][0], centre)[0];

          if (current < accumulator) acc = list[i];
        }
        if(getDist(acc[0], centre)[0] < 50){
          if(acc[2]){
            if(acc[2] == thisArg.fillStyle){
              acc[1]++;
            }else{
              acc[1] = 0;
            }
          }
          if(acc[1] > 2){
            return;
          }
          if(calls == 4){
            temptriangles.push([centre, acc[1], thisArg.fillStyle]);
          }
          if(calls == 5){
            tempsquares.push([centre, acc[1], thisArg.fillStyle]);
          }
          else {
            temppentagons.push([centre, acc[1], thisArg.fillStyle]);
          }
        }
      return;
    }
    if(playerPos && baseArea){
      const wcord = toWorldCoords(...centre);
      let distance;
      if(is2T){
        distance = getDist([baseArea.x, playerPos[1]], wcord);
      }
      else{
        distance = getDist([baseArea.x, baseArea.y], wcord);
      }

      if(distance[0] > 9000){
        return;
      }
    }
    if(calls == 4){
      temptriangles.push([centre, 0]);
    }
    if(calls == 5){
      tempsquares.push([centre, 0]);
    }
    if(calls == 6){
      temppentagons.push([centre, 0]);
    }
  } else {
    calls = 0;
  }
});

hook('strokeRect', function(thisArg, args) {
  const t = thisArg.getTransform();
  minimapPos = [t.e, t.f];
  minimapDim = [t.a, t.d];
});

hook('arc', function(thisArg, args){
  const t = thisArg.getTransform();

  if(!baseArea){
    return;
  }
  if(["#00b2e1", "#bf7ff5", "#00e16e", "#f14e54"].includes(thisArg.fillStyle)){
    if(baseArea.hex == thisArg.fillStyle){
      return;
    }
    enemies.push([[t.e, t.f], 0, thisArg.fillStyle])
  }
});


function getBase(pos){
  if(is2T){
    if(pos[0] < 2500){
      baseArea = T2_BASES[0];
      inBase = true;
      return;
    }
    if(pos[0] > 23500){
      baseArea = T2_BASES[1];
      inBase = true;
      return;
    }
    inBase = false;
    return;
  }
  else{
    for(let i = 0; i < T4_BASES.length; i++){
      if(pos[0] > T4_BASES[i].x && pos[0] < T4_BASES[i].x + T4_BASE_WIDTH && pos[1] > T4_BASES[i].y && pos[1] < T4_BASES[i].y + T4_BASE_HEIGHT){
        baseArea = T4_BASES[i];
        inBase = true;
        return;
      }
    }
    inBase = false;
    return;
  }
}
function getRandomLocation(){
  return [ARENA_WIDTH / 2, ARENA_HEIGHT / 2];
}
function getPlayerPos(){
  const dX = minimapArrow[0] - minimapPos[0];
  const dY = minimapArrow[1] - minimapPos[1];

  const x = (dX / minimapDim[0]) * ARENA_WIDTH;
  const y = (dY / minimapDim[1]) * ARENA_HEIGHT;

  return [x, y]
}


function getCurrentTargets(){
  let target = [0, 0];
  let moveTarget = [0, 0];
  let aimTarget = [0, 0];

  if(enemies.length){
    target = getClosest(enemies)[0];
    move(target, invertCoordinate(target));
    goal = null;
    return;
  }
  else if(pentagons.length){
    target = getClosest(pentagons)[0];
  }
  else if(triangles.length){
    target = getClosest(triangles)[0];
  }
  else if(squares.length){
    target = getClosest(squares)[0];
  }
  else{
    if(baseArea){
      const baseMidX = baseArea.x + T4_BASE_WIDTH / 2;
      const baseMidY = baseArea.y + T4_BASE_HEIGHT / 2;
      if(!goal){
        if(is2T){
          if(inBase){
            goal = [baseMidX + baseArea.dirX * T4_BASE_WIDTH * Math.random() * 1.5, Math.random() * ARENA_HEIGHT];
          }
          else{
            goal = [baseMidX, Math.random() * ARENA_HEIGHT];
          }
        }
        else{
          if(inBase){
            goal = [baseMidX + baseArea.dirX * T4_BASE_WIDTH * Math.random() * 1.5, baseMidY + baseArea.dirY * T4_BASE_HEIGHT * Math.random() * 1.5];
          }
          else{
            goal = [baseMidX, baseMidY];
          }
        }
      }
      moveToCoord(...goal, 0); // move inside base

      const goalDistance = getDist(goal, playerPos);
      if(goalDistance[0] < 175){
        goal = null;
      }
      return;
    }
    moveToCoord(...getRandomLocation(), 0);
    return;
  }
  const distance = getDist(target, [canvas.width / 2, canvas.height / 2]);
  aimTarget = target;


  if(distance[0] > 290){
    moveTarget = target;
  }
  else{
    moveTarget = invertCoordinate(target);
  }


  move(aimTarget, moveTarget);
}
function move(aimTarget, moveTarget){
  if(!window.input || !window.input.should_prevent_unload()){
    return;
  }
  window.input.mouse(...aimTarget);
  window.input.key_down(1);

  const moveTargetDistance = getDist(moveTarget, [canvas.width / 2, canvas.height / 2]);

  if(moveTargetDistance[1] > 0){ // x movement
    input.key_down(68);
    input.key_up(65);
  }
  else if(moveTargetDistance[1] < -0){
    input.key_up(68);
    input.key_down(65);
  }
  else{
    input.key_up(68);
    input.key_up(65);
  }





  if(moveTargetDistance[2] > 0){ // y movement
    input.key_down(83);
    input.key_up(87);
  }
  else if(moveTargetDistance[2] < -0){
    input.key_up(83);
    input.key_down(87);
  }
  else{
    input.key_up(83);
    input.key_up(87);
  }

  ctx.beginPath();
  ctx.lineWidth = 6;
  ctx.strokeStyle = "red";
  ctx.moveTo(canvas.width / 2, canvas.height / 2);
  ctx.lineTo(...aimTarget);
  ctx.stroke();
}
function toScreenCoords(x, y){
  const distance = getDist([x, y], playerPos);
  return [
    canvas.width / 2 + distance[1] / 2,
    canvas.height / 2 + distance[2] / 2,
  ]
}
function toWorldCoords(x, y){
  const distance = getDist([x, y], [canvas.width / 2, canvas.height / 2]);

  return [
    playerPos[0] + distance[1] * 2,
    playerPos[1] + distance[2] * 2,
  ]
}
function moveToCoord(x, y, invert){
  const distance = getDist([x, y], playerPos);

  if(distance[1] > 0){ // x movement
    input.key_down(68);
    input.key_up(65);
  }
  else if(distance[1] < -0){
    input.key_up(68);
    input.key_down(65);
  }
  else{
    input.key_up(68);
    input.key_up(65);
  }

  if(distance[2] > 0){ // y movement
    input.key_down(83);
    input.key_up(87);
  }
  else if(distance[2] < -0){
    input.key_up(83);
    input.key_down(87);
  }
  else{
    input.key_up(83);
    input.key_up(87);
  }

  const scrCoords = invert ? invertCoordinate(toScreenCoords(x, y)) : toScreenCoords(x, y);

  input.mouse(...scrCoords);
  ctx.beginPath();
  ctx.lineWidth = 6;
  ctx.strokeStyle = "lime";
  ctx.moveTo(canvas.width / 2, canvas.height / 2);
  ctx.lineTo(...scrCoords);
  ctx.stroke();
}

function main(){
  window.requestAnimationFrame(main);
  playerPos = getPlayerPos();
  getBase(playerPos);

  if(Date.now() - lastCheck > 2000){
    is2T = true;
  }
  else{
    is2T = false;
  }

  if(toggled){
    getCurrentTargets();
    if(!input.should_prevent_unload()){
      window.input.try_spawn(localStorage.name);
    }
  }


  squares = tempsquares;
  triangles = temptriangles;
  pentagons = temppentagons;
  tempsquares = [];
  temptriangles = [];
  temppentagons = [];
  enemies = [];
}

window.requestAnimationFrame(main);
}, 400);


const handler = {
  apply(r,o,args) {
    Error.stackTraceLimit = 0;
    return r.apply(o,args)
  }
}
Object.freeze = new Proxy(Object.freeze, handler)