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