Base Zones

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

当前为 2021-10-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Base Zones
  3. // @description credits to cazka for basically 99% of this script, no this is not a multiboxing script
  4. // @version 1.0.1
  5. // @author none
  6. // @match *://diep.io/*
  7. // @grant GM_addStyle
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_addValueChangeListener
  11. // @grant GM_removeValueChangeListener
  12. // @namespace https://greasyfork.org/users/790354
  13. // ==/UserScript==
  14. 'use strict';
  15. //set scale here
  16. const scales = 0.5
  17.  
  18. class Gui {
  19. constructor(title) {
  20. this._buttons = [];
  21. this._notifications = [];
  22.  
  23. this._title = title;
  24. this._gui;
  25. this._guiHead;
  26. this._guiBody;
  27. this._notificationBody;
  28. this._notificationColor = 0;
  29.  
  30. this._init();
  31. this._enableShortcuts();
  32. }
  33. _init() {
  34. const nonce = `a${Math.floor(Math.random() * 1e5)}`;
  35. GM_addStyle(
  36. `.${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)}`
  37. );
  38. }
  39.  
  40. addButton(text, onclick, keyCode) {
  41. return this._addButton(this._guiBody, text, onclick, keyCode);
  42. }
  43.  
  44. notification(text, duration = 1) {
  45. const button = document.createElement('button');
  46. button.innerText = text;
  47. button.style['background-color'] = this._colors[this._notificationColor++ % this._colors.length];
  48. button.style.display = 'block';
  49. button.addEventListener('contextmenu', (e) => e.preventDefault());
  50.  
  51. this._notificationBody.appendChild(button);
  52. setTimeout(() => button.remove(), duration);
  53. }
  54.  
  55. _addButton(parent, text, onclick, keyCode) {
  56. const button = document.createElement('button');
  57. button.innerHTML = text;
  58. button.keyCode = keyCode;
  59. button.onclick = onclick;
  60. button.style['background-color'] = this._colors[this._buttons.length % this._colors.length];
  61. button.addEventListener('contextmenu', (e) => e.preventDefault());
  62.  
  63. parent.appendChild(button);
  64. this._buttons.push(button);
  65. return button;
  66. }
  67.  
  68. _enableShortcuts() {
  69. unsafeWindow.onkeydown = new Proxy(unsafeWindow.onkeydown, {
  70. apply: (target, thisArgs, args) => {
  71. if (document.getElementById('textInputContainer').style.display === 'block') {
  72. return Reflect.apply(target, thisArgs, args);
  73. }
  74. this._buttons.forEach((button) => {
  75. if (button.keyCode === event.code) button.onclick();
  76. });
  77. return Reflect.apply(target, thisArgs, args);
  78. },
  79. });
  80. }
  81. }
  82. class Minimap {
  83. constructor() {
  84. this._minimapWidth;
  85. this._minimapHeight;
  86. this._x00;
  87. this._y00;
  88. this._pointX;
  89. this._pointY;
  90. this._pointX_previous;
  91. this._pointY_previous;
  92. this._viewportWidth;
  93. this._viewportHeight;
  94. this._fov;
  95.  
  96. this._minimapHook();
  97. this._arrowHook();
  98. this._viewportHook();
  99. this._fovHook();
  100. this.name = this._minimapWidth / 129
  101. }
  102. get x() {
  103. return this._pointX ? (this._pointX - this._x00) / this._minimapWidth : 0;
  104. }
  105. get y() {
  106. return this._pointY ? (this._pointY - this._y00) / this._minimapHeight : 0;
  107. }
  108. get x_previous() {
  109. return this._pointX_previous ? (this._pointX_previous - this._x00) / this._minimapWidth : 0;
  110. }
  111. get y_previous() {
  112. return this._pointY_previous ? (this._pointY_previous - this._y00) / this._minimapHeight : 0;
  113. }
  114. get scale() {
  115. return {
  116. x: this._viewportWidth / this._minimapWidth,
  117. y: this._viewportHeight / this._minimapHeight,
  118. };
  119. }
  120. get scale1() {
  121. return this._minimapWidth / 129;
  122. }
  123. get fov() {
  124. return this._fov;
  125. }
  126.  
  127. _minimapHook() {
  128. let setTransformArgs;
  129.  
  130. const onsetTransform = (args) => {
  131. if (args[0] === args[3]) setTransformArgs = args;
  132. };
  133. const onstrokeRect = () => {
  134. if (setTransformArgs) {
  135. this._minimapWidth = setTransformArgs[0];
  136. this._minimapHeight = setTransformArgs[3];
  137. this._x00 = setTransformArgs[4];
  138. this._y00 = setTransformArgs[5];
  139. setTransformArgs = undefined;
  140. }
  141. };
  142. this._ctxHook('setTransform', onsetTransform);
  143. this._ctxHook('strokeRect', onstrokeRect);
  144. }
  145. _arrowHook() {
  146. let index = 0;
  147. const stack = Array(4);
  148.  
  149. let pointA;
  150. let pointB;
  151. let pointC;
  152.  
  153. const calculatePos = () => {
  154. const side1 = Math.floor(
  155. Math.sqrt(Math.pow(pointA[0] - pointB[0], 2) + Math.pow(pointA[1] - pointB[1], 2))
  156. );
  157. const side2 = Math.floor(
  158. Math.sqrt(Math.pow(pointA[0] - pointC[0], 2) + Math.pow(pointA[1] - pointC[1], 2))
  159. );
  160. const side3 = Math.floor(
  161. Math.sqrt(Math.pow(pointB[0] - pointC[0], 2) + Math.pow(pointB[1] - pointC[1], 2))
  162. );
  163. if (side1 == side2 && side2 == side3) return;
  164.  
  165. this._pointX_previous = this._pointX;
  166. this._pointY_previous = this._pointY;
  167.  
  168. this._pointX = (pointA[0] + pointB[0] + pointC[0]) / 3;
  169. this._pointY = (pointA[1] + pointB[1] + pointC[1]) / 3;
  170. };
  171. const onbeginPath = () => {
  172. index = 0;
  173. stack[index++] = 0;
  174. };
  175. const onmoveTo = (args) => {
  176. if (index === 1 && stack[index - 1] === 0) {
  177. stack[index++] = 1;
  178. pointA = args;
  179. return;
  180. }
  181. index = 0;
  182. };
  183. const onlineTo = (args) => {
  184. if (index === 2 && stack[index - 1] === 1) {
  185. stack[index++] = 2;
  186. pointB = args;
  187. return;
  188. }
  189. if (index === 3 && stack[index - 1] === 2) {
  190. stack[index++] = 2;
  191. pointC = args;
  192. return;
  193. }
  194. index = 0;
  195. };
  196. const onfill = () => {
  197. if (index === 4 && stack[index - 1] === 2) {
  198. calculatePos();
  199. return;
  200. }
  201. index = 0;
  202. };
  203.  
  204. this._ctxHook('beginPath', onbeginPath);
  205. this._ctxHook('moveTo', onmoveTo);
  206. this._ctxHook('lineTo', onlineTo);
  207. this._ctxHook('fill', onfill);
  208. }
  209. _viewportHook() {
  210. let setTransformArgs;
  211.  
  212. const onsetTransform = (args) => {
  213. if ((args[0] / args[3]).toFixed(4) !== (unsafeWindow.innerWidth / unsafeWindow.innerHeight).toFixed(4))
  214. return;
  215. if (args[0] >= unsafeWindow.innerWidth && args[3] >= unsafeWindow.innerHeight) return;
  216.  
  217. setTransformArgs = args;
  218. };
  219. const onfillRect = () => {
  220. if (setTransformArgs) {
  221. unsafeWindow.input.set_convar('ren_minimap_viewport', false);
  222. this._viewportWidth = setTransformArgs[0];
  223. this._viewportHeight = setTransformArgs[3];
  224. setTransformArgs = undefined;
  225. }
  226. };
  227.  
  228. this._ctxHook('setTransform', onsetTransform);
  229. this._ctxHook('fillRect', onfillRect);
  230.  
  231. setTimeout(() => unsafeWindow.input.set_convar('ren_minimap_viewport', true), 1000);
  232. setInterval(() => {
  233. unsafeWindow.input.set_convar('ren_minimap_viewport', true);
  234. }, 1000);
  235. }
  236. _fovHook() {
  237. let solid_background = false;
  238. setTimeout(() => {
  239. solid_background = unsafeWindow.input.get_convar('ren_solid_background') === 'true' ? true : false;
  240. }, 1000);
  241.  
  242. const calculateFov = (fov) => {
  243. this._fov = fov * 10;
  244. };
  245. function onstroke() {
  246. if (this.fillStyle === '#cdcdcd') {
  247. if (solid_background) unsafeWindow.input.set_convar('ren_solid_background', true);
  248. calculateFov(this.globalAlpha);
  249. }
  250. }
  251.  
  252. this._ctxHook('stroke', onstroke);
  253.  
  254. setInterval(() => {
  255. if (solid_background) unsafeWindow.input.set_convar('ren_solid_background', false);
  256. }, 1000);
  257. }
  258. _ctxHook(method, hook) {
  259. const target = window.CanvasRenderingContext2D.prototype;
  260. target[method] = new Proxy(target[method], {
  261. apply(target, thisArg, args) {
  262. args = hook.call(thisArg, args) || args;
  263. return target.apply(thisArg, args);
  264. },
  265. });
  266. }
  267. }
  268. class DiepGamepad {
  269. constructor() {
  270. this._axes = [0, 0, 0, 0];
  271. this._buttons = [...Array(17)].map((x) => {
  272. return { pressed: false };
  273. });
  274. }
  275.  
  276. set x(value) {
  277. this._axes[0] = value;
  278. }
  279. set y(value) {
  280. this._axes[1] = value;
  281. }
  282. set mx(value) {
  283. this._axes[2] = value;
  284. }
  285. set my(value) {
  286. this._axes[3] = value;
  287. }
  288. set leftMouse(value) {
  289. this._buttons[7].pressed = value;
  290. }
  291. set rightMouse(value) {
  292. this._buttons[6].pressed = value;
  293. }
  294. set connected(value) {
  295. unsafeWindow.navigator.getGamepads = () => [value ? this.toGamepad() : undefined];
  296. }
  297.  
  298. get x() {
  299. return this._axes[0];
  300. }
  301. get y() {
  302. return this._axes[1];
  303. }
  304. get mx() {
  305. return this._axes[2];
  306. }
  307. get my() {
  308. return this._axes[3];
  309. }
  310. get leftMouse() {
  311. return this._buttons[7].pressed;
  312. }
  313. get rightMouse() {
  314. return this._buttons[6].pressed;
  315. }
  316. get connected() {
  317. return unsafeWindow.navigator.getGamepads()[0] ? true : false;
  318. }
  319.  
  320. toGamepad() {
  321. return {
  322. axes: this._axes,
  323. buttons: this._buttons,
  324. mapping: 'standard',
  325. };
  326. }
  327. }
  328. class Vector {
  329.  
  330. }
  331. class Arena {
  332. static get BLOCKSIZE() {
  333. return 50;
  334. }
  335. static scale(x, y) {
  336. return {
  337. x: Math.floor(22300 * (x - 0.5) + 0.5),
  338. y: Math.floor(22300 * (y - 0.5) + 0.5),
  339. };
  340. }
  341. static unscale(x, y) {
  342. return {
  343. x: x / 22300 + 0.5,
  344. y: y / 22300 + 0.5,
  345. };
  346. }
  347. }
  348. class Player {
  349. constructor() {
  350. this._minimap = new Minimap();
  351. this._gamepad = new DiepGamepad();
  352.  
  353. this._mouse = {
  354. x: 0,
  355. y: 0,
  356. };
  357. this._inputs = {
  358. left: false,
  359. down: false,
  360. up: false,
  361. right: false,
  362. };
  363. this._dead = true;
  364.  
  365. unsafeWindow.addEventListener('mousemove', (e) => this._onmousemove(e));
  366. unsafeWindow.addEventListener('mousedown', (e) => this._onmousedown(e));
  367. unsafeWindow.addEventListener('mouseup', (e) => this._onmouseup(e));
  368. unsafeWindow.addEventListener('keydown', (e) => this._onkeydown(e));
  369. unsafeWindow.addEventListener('keyup', (e) => this._onkeyup(e));
  370. //Dead Listener
  371. new MutationObserver((args) => {
  372. this._dead = args[0].target.style.display === 'block';
  373. if (this.ondead && this._dead) this.ondead();
  374. }).observe(document.getElementById('a'), { attributes: true });
  375. //Message Listener
  376. const notify = (text) => {
  377. if (this.onmessage) this.onmessage(text);
  378. };
  379. CanvasRenderingContext2D.prototype.fillText = new Proxy(CanvasRenderingContext2D.prototype.fillText, {
  380. apply(target, thisArg, args) {
  381. if (args[0].startsWith("You've killed ")) {
  382. notify(args[0]);
  383. }
  384. return Reflect.apply(target, thisArg, args);
  385. },
  386. });
  387. }
  388.  
  389. set useGamepad(value) {
  390. this._gamepad.connected = value;
  391. }
  392.  
  393. get position() {
  394. const position = Arena.scale(this._minimap.x, this._minimap.y);
  395. const previous = Arena.scale(this._minimap.x_previous, this._minimap.y_previous);
  396. return {
  397. x: position.x,
  398. y: position.y,
  399. x_previous: previous.x,
  400. y_previous: previous.y,
  401. };
  402. }
  403. get mouse() {
  404. return this.toArenaPos(this._mouse.x, this._mouse.y);
  405. }
  406. get inputs() {
  407. return this._inputs;
  408. }
  409. get dead() {
  410. return this._dead;
  411. }
  412.  
  413. get gamemode() {
  414. return unsafeWindow.localStorage.gamemode;
  415. }
  416.  
  417. keyDown(key) {
  418. unsafeWindow.input.keyDown(key);
  419. this._onkeydown({ keyCode: key });
  420. }
  421. keyUp(key) {
  422. unsafeWindow.input.keyUp(key);
  423. this._onkeyup({ keyCode: key });
  424. }
  425.  
  426. toScreenPos(x, y) {
  427. const unscale = Arena.unscale(x, y);
  428. x = unscale.x;
  429. y = unscale.y;
  430.  
  431. let position = this.position;
  432. position = Arena.unscale(position.x, position.y);
  433. const scale = this._minimap.scale;
  434.  
  435. const a = window.innerHeight / 1080;
  436. const b = window.innerWidth / 1920;
  437. const c = b < a ? a : b;
  438. const unknown = this._minimap.fov * c;
  439.  
  440. x -= position.x;
  441. x /= scale.x;
  442. x += 0.5;
  443. x *= unsafeWindow.innerWidth;
  444.  
  445. y -= position.y;
  446. y /= scale.y;
  447. y += 0.5;
  448. y *= unsafeWindow.innerHeight;
  449.  
  450. return { x, y };
  451. }
  452.  
  453. }
  454. class AAAAAA {
  455.  
  456. }
  457. /**
  458. *this code is useless, but do not delete as that messes up something
  459. */
  460. class Chat {
  461. constructor(player) {
  462. this._player = player;
  463. this._inputBox = document.body.appendChild(document.createElement('div'));
  464. this._hookonkeydown();
  465. this._hookAnimationFrame();
  466. }
  467. _onkeydown(e) {
  468. let preventDefault = false;
  469.  
  470. if (player.dead) return;
  471.  
  472. return preventDefault;
  473. }
  474. _hookonkeydown() {
  475. const _this = this;
  476. unsafeWindow.onkeydown = new Proxy(unsafeWindow.onkeydown, {
  477. });
  478. }
  479. _hookAnimationFrame() {
  480. const _this = this;
  481. unsafeWindow.requestAnimationFrame = new Proxy(unsafeWindow.requestAnimationFrame, {
  482. });
  483. }
  484. }
  485.  
  486.  
  487.  
  488. function drawZones(scale) {
  489. if (player.dead) return;
  490. ctx.save();
  491. ctx.globalAlpha = 0.08;
  492. let radius = 45
  493.  
  494. if (player.gamemode === '4teams') {
  495.  
  496. //blue
  497. ctx.fillStyle = '#006480';
  498. ctx.beginPath();
  499. ctx.fillRect(1920-(1920-1728)*scale, 1080-(1080-888)*scale, radius*scale, radius*scale);
  500.  
  501. //purple
  502. ctx.fillStyle = '#644280';
  503. ctx.beginPath();
  504. ctx.fillRect(1920-(1920-1728)*scale+128*scale, 1080-(1080-888)*scale, radius*scale, radius*scale);
  505.  
  506. //green
  507. ctx.fillStyle = '#00803e';
  508. ctx.beginPath();
  509. ctx.fillRect(1920-(1920-1728)*scale, 1080-(1080-888)*scale+128*scale, radius*scale, radius*scale);
  510.  
  511. //red
  512. ctx.fillStyle = '#963033';
  513. ctx.beginPath();
  514. ctx.fillRect(1920-(1920-1728)*scale+129*scale, 1080-(1080-888)*scale+128*scale, radius*scale, radius*scale);
  515. } else if (player.gamemode === 'teams') {
  516. let radius2 = 40;
  517. //blue
  518. ctx.fillStyle = '#006480';
  519. ctx.fillRect(1920-(1920-1728)*scale, 1080-(1080-888)*scale, radius2*scale, 170*scale);
  520.  
  521. //red
  522. ctx.fillStyle = '#963033';
  523. ctx.fillRect(1920-(1920-1728)*scale+128*scale, 1080-(1080-888)*scale, radius2*scale, 170*scale);
  524. }
  525. ctx.restore();
  526. }
  527.  
  528. const player = new Player();
  529. const storage = new AAAAAA();
  530. const chat = new Chat(player);
  531. //setup canvas
  532. const ctx = document.getElementById('canvas').getContext('2d');
  533.  
  534. // run main Loop
  535. unsafeWindow.requestAnimationFrame = new Proxy(unsafeWindow.requestAnimationFrame, {
  536. apply: function (target, thisArg, args) {
  537. drawZones(scales);
  538. if (player.isMaster) return Reflect.apply(target, thisArg, args);
  539. else setTimeout(() => Reflect.apply(target, thisArg, args), 0);
  540. },
  541. });