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

  1. // ==UserScript==
  2. // @name Diep.io Auto-leveler Bot
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-03-24 - 2024-04-14
  5. // @description Press Q to toggle, must be in base at least once in a lifetime so the script knows what team you are in
  6. // @author Mi300
  7. // @match https://diep.io/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @license Apache License 2.0
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14.  
  15.  
  16. /* --------HOW TO USE--------
  17. // 1. Use in 2tdm or 4tdm. in any other gamemode, it breaks!
  18. // 2. Press Q to toggle on / off
  19. // 3. Player must be in their base at least once so the script knows what team they are in
  20. // 4. If you get disconnected by the anti-cheat just refresh the page it should fix it
  21. // 5. The script is only functional if you are on the tab or if the tab is a seperate window
  22. // 6. If you see any issues tell me on discord (Mi300), but I probably won't do anything about it
  23. //
  24. // --------------------------
  25. */
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. const ARENA_WIDTH = 26000;
  40. const ARENA_HEIGHT = 26000;
  41.  
  42. const T4_BASE_WIDTH = 3900;
  43. const T4_BASE_HEIGHT = 3900;
  44.  
  45. const T2_BASES = [
  46. {
  47. id: 0,
  48. name: "blue",
  49. hex: "#00b2e1",
  50. x: 0,
  51. y: 0,
  52. cX: 0,
  53. cY: 0,
  54. dirX: 1,
  55. dirY: 1,
  56. },
  57. {
  58. id: 3,
  59. name: "red",
  60. hex: "#f14e54",
  61. x: 23500,
  62. y: 0,
  63. cX: ARENA_WIDTH,
  64. cY: ARENA_HEIGHT,
  65. dirX: -1,
  66. dirY: 1,
  67. },
  68. ]
  69.  
  70. const T4_BASES = [
  71. {
  72. id: 0,
  73. name: "blue",
  74. hex: "#00b2e1",
  75. x: 0,
  76. y: 0,
  77. cX: 0,
  78. cY: 0,
  79. dirX: 1,
  80. dirY: 1,
  81. },
  82. {
  83. id: 1,
  84. name: "purple",
  85. hex: "#bf7ff5",
  86. x: 22100,
  87. y: 0,
  88. cX: 0,
  89. cY: ARENA_HEIGHT,
  90. dirX: -1,
  91. dirY: 1,
  92. },
  93. {
  94. id: 2,
  95. name: "green",
  96. hex: "#00e16e",
  97. x: 0,
  98. y: 22100,
  99. cX: ARENA_WIDTH,
  100. cY: 0,
  101. dirX: 1,
  102. dirY: -1,
  103. },
  104. {
  105. id: 3,
  106. name: "red",
  107. hex: "#f14e54",
  108. x: 22100,
  109. y: 22100,
  110. cX: ARENA_WIDTH,
  111. cY: ARENA_HEIGHT,
  112. dirX: -1,
  113. dirY: -1,
  114. },
  115. ]
  116.  
  117. alert("Auto Leveler: Press Q to toggle on / off.")
  118.  
  119. let OMG = setInterval(function(){
  120. if(!window.input){
  121. return;
  122. }
  123. clearInterval(OMG);
  124.  
  125. const canvas = document.getElementById("canvas");
  126. const ctx = canvas.getContext("2d");
  127. let colors = {
  128. minimapArrow: '#000000',
  129. players: '#f14e54',
  130. squares: '#ffe869',
  131. triangles: '#fc7677',
  132. pentagons: '#768dfc',
  133. alphaPentagons: '#768dfc',
  134. bullets: '#f14e54',
  135. drones: '#f14e54',
  136. crashers: '#f177dd',
  137. necromancerDrone: '#fcc376',
  138. }
  139. let enemies = [];
  140. let squares = [];
  141. let tempsquares = [];
  142. let triangles = [];
  143. let temptriangles = [];
  144. let pentagons = [];
  145. let temppentagons = [];
  146. let dead = true;
  147. let baseArea;
  148. let inBase = true;
  149. let goal;
  150. let toggled = false;
  151. let lastCheck = Date.now();
  152. let is2T = false;
  153.  
  154. let minimapArrow = [0, 0];
  155. let minimapPos = [0, 0];
  156. let minimapDim = [0, 0];
  157. let playerPos = [0, 0];
  158.  
  159. document.addEventListener("keydown", function(e){
  160. if(e.key == "q"){
  161. toggled = !toggled;
  162. setTimeout(function(){
  163. input.key_up(87);
  164. input.key_up(83);
  165. input.key_up(65);
  166. input.key_up(68);
  167. },200);
  168. }
  169. });
  170.  
  171. function getCentre(vertices) {
  172. let centre = [0, 0];
  173. vertices.forEach (vertex => {
  174. centre [0] += vertex[0]
  175. centre [1] += vertex[1]
  176. });
  177. centre[0] /= vertices.length;
  178. centre[1] /= vertices.length;
  179. return centre;
  180. }
  181. function getClosest(entities) {
  182. let acc = [[0, 0], 0]
  183. for (let i = 0; i < entities.length; i ++) {
  184. const accumulator = getDist (acc[0], [canvas.width / 2, canvas.height / 2])[0];
  185. const current = getDist (entities[i][0], [canvas.width / 2, canvas.height / 2])[0];
  186.  
  187. if (current < accumulator) acc = entities[i];
  188. }
  189. return acc;
  190. }
  191. function invertCoordinate(coord){
  192. return [canvas.width - coord[0], canvas.height - coord[1]];
  193. }
  194. function getDist(t1, t2){
  195. const distX = t1[0] - t2[0];
  196. const distY = t1[1] - t2[1];
  197.  
  198. return [Math.hypot(distX, distY), distX, distY];
  199. };
  200.  
  201. function hook(target, callback){
  202.  
  203. function check(){
  204. window.requestAnimationFrame(check)
  205.  
  206. const func = CanvasRenderingContext2D.prototype[target]
  207.  
  208. if(func.toString().includes(target)){
  209.  
  210. CanvasRenderingContext2D.prototype[target] = new Proxy (func, {
  211. apply (method, thisArg, args) {
  212. callback(thisArg, args)
  213.  
  214. return Reflect.apply (method, thisArg, args)
  215. }
  216. });
  217. }
  218. }
  219. window.requestAnimationFrame(check)
  220. }
  221.  
  222. let calls = 0;
  223. let points = [];
  224. hook('beginPath', function(thisArg, args){
  225. calls = 1;
  226. points = [];
  227. });
  228. hook('moveTo', function(thisArg, args){
  229. if (calls == 1) {
  230. calls+=1;
  231. points.push(args)
  232. } else {
  233. calls = 0;
  234. }
  235. });
  236. hook('lineTo', function(thisArg, args){
  237. if (calls >= 2 && calls <= 6) {
  238. calls+=1;
  239. points.push(args)
  240. } else {
  241. calls = 0;
  242. }
  243. });
  244. hook('fill', function(thisArg, args){
  245. if(thisArg.fillStyle == "#00e16e"){
  246. lastCheck = Date.now();
  247. }
  248.  
  249. if(calls >= 4 && calls <= 6) {
  250. const centre = getCentre(points);
  251. const list = calls == 4 ? triangles : calls == 5 ? squares : pentagons;
  252.  
  253.  
  254. if(thisArg.globalAlpha < 1){
  255. return;
  256. }
  257. if(thisArg.fillStyle == "#000000"){
  258. if(true){
  259. minimapArrow = centre;
  260. }
  261.  
  262. return;
  263. }
  264. if(!baseArea){
  265. return;
  266. }
  267. if((calls == 5 || calls == 4) && ["#00b2e1", "#bf7ff5", "#00e16e", "#f14e54"].includes(thisArg.fillStyle)){
  268. if(baseArea.hex == thisArg.fillStyle){
  269. return;
  270. }
  271. enemies.push([centre, 0, thisArg.fillStyle])
  272. return;
  273. }
  274.  
  275. if (!['#ffe869', '#fc7677', '#768dfc'].includes(thisArg.fillStyle)) {
  276. let acc = [[0, 0], 0]
  277. for (let i = 0; i < list.length; i ++) {
  278. const accumulator = getDist (acc[0], centre)[0];
  279. const current = getDist (list[i][0], centre)[0];
  280.  
  281. if (current < accumulator) acc = list[i];
  282. }
  283. if(getDist(acc[0], centre)[0] < 50){
  284. if(acc[2]){
  285. if(acc[2] == thisArg.fillStyle){
  286. acc[1]++;
  287. }else{
  288. acc[1] = 0;
  289. }
  290. }
  291. if(acc[1] > 2){
  292. return;
  293. }
  294. if(calls == 4){
  295. temptriangles.push([centre, acc[1], thisArg.fillStyle]);
  296. }
  297. if(calls == 5){
  298. tempsquares.push([centre, acc[1], thisArg.fillStyle]);
  299. }
  300. else {
  301. temppentagons.push([centre, acc[1], thisArg.fillStyle]);
  302. }
  303. }
  304. return;
  305. }
  306. if(playerPos && baseArea){
  307. const wcord = toWorldCoords(...centre);
  308. let distance;
  309. if(is2T){
  310. distance = getDist([baseArea.x, playerPos[1]], wcord);
  311. }
  312. else{
  313. distance = getDist([baseArea.x, baseArea.y], wcord);
  314. }
  315.  
  316. if(distance[0] > 9000){
  317. return;
  318. }
  319. }
  320. if(calls == 4){
  321. temptriangles.push([centre, 0]);
  322. }
  323. if(calls == 5){
  324. tempsquares.push([centre, 0]);
  325. }
  326. if(calls == 6){
  327. temppentagons.push([centre, 0]);
  328. }
  329. } else {
  330. calls = 0;
  331. }
  332. });
  333.  
  334. hook('strokeRect', function(thisArg, args) {
  335. const t = thisArg.getTransform();
  336. minimapPos = [t.e, t.f];
  337. minimapDim = [t.a, t.d];
  338. });
  339.  
  340. hook('arc', function(thisArg, args){
  341. const t = thisArg.getTransform();
  342.  
  343. if(!baseArea){
  344. return;
  345. }
  346. if(["#00b2e1", "#bf7ff5", "#00e16e", "#f14e54"].includes(thisArg.fillStyle)){
  347. if(baseArea.hex == thisArg.fillStyle){
  348. return;
  349. }
  350. enemies.push([[t.e, t.f], 0, thisArg.fillStyle])
  351. }
  352. });
  353.  
  354.  
  355. function getBase(pos){
  356. if(is2T){
  357. if(pos[0] < 2500){
  358. baseArea = T2_BASES[0];
  359. inBase = true;
  360. return;
  361. }
  362. if(pos[0] > 23500){
  363. baseArea = T2_BASES[1];
  364. inBase = true;
  365. return;
  366. }
  367. inBase = false;
  368. return;
  369. }
  370. else{
  371. for(let i = 0; i < T4_BASES.length; i++){
  372. 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){
  373. baseArea = T4_BASES[i];
  374. inBase = true;
  375. return;
  376. }
  377. }
  378. inBase = false;
  379. return;
  380. }
  381. }
  382. function getRandomLocation(){
  383. return [ARENA_WIDTH / 2, ARENA_HEIGHT / 2];
  384. }
  385. function getPlayerPos(){
  386. const dX = minimapArrow[0] - minimapPos[0];
  387. const dY = minimapArrow[1] - minimapPos[1];
  388.  
  389. const x = (dX / minimapDim[0]) * ARENA_WIDTH;
  390. const y = (dY / minimapDim[1]) * ARENA_HEIGHT;
  391.  
  392. return [x, y]
  393. }
  394.  
  395.  
  396. function getCurrentTargets(){
  397. let target = [0, 0];
  398. let moveTarget = [0, 0];
  399. let aimTarget = [0, 0];
  400.  
  401. if(enemies.length){
  402. target = getClosest(enemies)[0];
  403. move(target, invertCoordinate(target));
  404. goal = null;
  405. return;
  406. }
  407. else if(pentagons.length){
  408. target = getClosest(pentagons)[0];
  409. }
  410. else if(triangles.length){
  411. target = getClosest(triangles)[0];
  412. }
  413. else if(squares.length){
  414. target = getClosest(squares)[0];
  415. }
  416. else{
  417. if(baseArea){
  418. const baseMidX = baseArea.x + T4_BASE_WIDTH / 2;
  419. const baseMidY = baseArea.y + T4_BASE_HEIGHT / 2;
  420. if(!goal){
  421. if(is2T){
  422. if(inBase){
  423. goal = [baseMidX + baseArea.dirX * T4_BASE_WIDTH * Math.random() * 1.5, Math.random() * ARENA_HEIGHT];
  424. }
  425. else{
  426. goal = [baseMidX, Math.random() * ARENA_HEIGHT];
  427. }
  428. }
  429. else{
  430. if(inBase){
  431. goal = [baseMidX + baseArea.dirX * T4_BASE_WIDTH * Math.random() * 1.5, baseMidY + baseArea.dirY * T4_BASE_HEIGHT * Math.random() * 1.5];
  432. }
  433. else{
  434. goal = [baseMidX, baseMidY];
  435. }
  436. }
  437. }
  438. moveToCoord(...goal, 0); // move inside base
  439.  
  440. const goalDistance = getDist(goal, playerPos);
  441. if(goalDistance[0] < 175){
  442. goal = null;
  443. }
  444. return;
  445. }
  446. moveToCoord(...getRandomLocation(), 0);
  447. return;
  448. }
  449. const distance = getDist(target, [canvas.width / 2, canvas.height / 2]);
  450. aimTarget = target;
  451.  
  452.  
  453. if(distance[0] > 290){
  454. moveTarget = target;
  455. }
  456. else{
  457. moveTarget = invertCoordinate(target);
  458. }
  459.  
  460.  
  461. move(aimTarget, moveTarget);
  462. }
  463. function move(aimTarget, moveTarget){
  464. if(!window.input || !window.input.should_prevent_unload()){
  465. return;
  466. }
  467. window.input.mouse(...aimTarget);
  468. window.input.key_down(1);
  469.  
  470. const moveTargetDistance = getDist(moveTarget, [canvas.width / 2, canvas.height / 2]);
  471.  
  472. if(moveTargetDistance[1] > 0){ // x movement
  473. input.key_down(68);
  474. input.key_up(65);
  475. }
  476. else if(moveTargetDistance[1] < -0){
  477. input.key_up(68);
  478. input.key_down(65);
  479. }
  480. else{
  481. input.key_up(68);
  482. input.key_up(65);
  483. }
  484.  
  485.  
  486.  
  487.  
  488.  
  489. if(moveTargetDistance[2] > 0){ // y movement
  490. input.key_down(83);
  491. input.key_up(87);
  492. }
  493. else if(moveTargetDistance[2] < -0){
  494. input.key_up(83);
  495. input.key_down(87);
  496. }
  497. else{
  498. input.key_up(83);
  499. input.key_up(87);
  500. }
  501.  
  502. ctx.beginPath();
  503. ctx.lineWidth = 6;
  504. ctx.strokeStyle = "red";
  505. ctx.moveTo(canvas.width / 2, canvas.height / 2);
  506. ctx.lineTo(...aimTarget);
  507. ctx.stroke();
  508. }
  509. function toScreenCoords(x, y){
  510. const distance = getDist([x, y], playerPos);
  511. return [
  512. canvas.width / 2 + distance[1] / 2,
  513. canvas.height / 2 + distance[2] / 2,
  514. ]
  515. }
  516. function toWorldCoords(x, y){
  517. const distance = getDist([x, y], [canvas.width / 2, canvas.height / 2]);
  518.  
  519. return [
  520. playerPos[0] + distance[1] * 2,
  521. playerPos[1] + distance[2] * 2,
  522. ]
  523. }
  524. function moveToCoord(x, y, invert){
  525. const distance = getDist([x, y], playerPos);
  526.  
  527. if(distance[1] > 0){ // x movement
  528. input.key_down(68);
  529. input.key_up(65);
  530. }
  531. else if(distance[1] < -0){
  532. input.key_up(68);
  533. input.key_down(65);
  534. }
  535. else{
  536. input.key_up(68);
  537. input.key_up(65);
  538. }
  539.  
  540. if(distance[2] > 0){ // y movement
  541. input.key_down(83);
  542. input.key_up(87);
  543. }
  544. else if(distance[2] < -0){
  545. input.key_up(83);
  546. input.key_down(87);
  547. }
  548. else{
  549. input.key_up(83);
  550. input.key_up(87);
  551. }
  552.  
  553. const scrCoords = invert ? invertCoordinate(toScreenCoords(x, y)) : toScreenCoords(x, y);
  554.  
  555. input.mouse(...scrCoords);
  556. ctx.beginPath();
  557. ctx.lineWidth = 6;
  558. ctx.strokeStyle = "lime";
  559. ctx.moveTo(canvas.width / 2, canvas.height / 2);
  560. ctx.lineTo(...scrCoords);
  561. ctx.stroke();
  562. }
  563.  
  564. function main(){
  565. window.requestAnimationFrame(main);
  566. playerPos = getPlayerPos();
  567. getBase(playerPos);
  568.  
  569. if(Date.now() - lastCheck > 2000){
  570. is2T = true;
  571. }
  572. else{
  573. is2T = false;
  574. }
  575.  
  576. if(toggled){
  577. getCurrentTargets();
  578. if(!input.should_prevent_unload()){
  579. window.input.try_spawn(localStorage.name);
  580. }
  581. }
  582.  
  583.  
  584. squares = tempsquares;
  585. triangles = temptriangles;
  586. pentagons = temppentagons;
  587. tempsquares = [];
  588. temptriangles = [];
  589. temppentagons = [];
  590. enemies = [];
  591. }
  592.  
  593. window.requestAnimationFrame(main);
  594. }, 400);
  595.  
  596.  
  597. const handler = {
  598. apply(r,o,args) {
  599. Error.stackTraceLimit = 0;
  600. return r.apply(o,args)
  601. }
  602. }
  603. Object.freeze = new Proxy(Object.freeze, handler)
  604.  
  605.  
  606.