Base Zones

diep.io base zone script

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

  1. // ==UserScript==
  2. // @name Base Zones
  3. // @description diep.io base zone script
  4. // @version 1.3.0
  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. function drawZones(x, y) {
  16. if (player.dead) return;
  17. let r = 52 * fov;
  18. let corner1 = 2600 * fov;
  19. let corner2 = 13245 * fov;
  20. ctx.save();
  21. ctx.globalAlpha = 0.04;
  22. if (player.gamemode === '4teams') {
  23. //Calibration
  24. //Blue
  25. ctx.fillStyle = '#000000';
  26. ctx.beginPath();
  27. ctx.arc(corner1-x, corner1-y, 65, 0, 2*Math.PI);
  28. ctx.fill();
  29. //Purple
  30. ctx.fillStyle = '#000000';
  31. ctx.beginPath();
  32. ctx.arc(corner2-x, corner1-y, 65, 0, 2*Math.PI);
  33. ctx.fill();
  34. //Green
  35. ctx.fillStyle = '#000000';
  36. ctx.beginPath;
  37. ctx.arc(corner1-x, corner2-y, 65, 0, 2*Math.PI);
  38. ctx.fill();
  39. //Red
  40. ctx.fillStyle = '#000000';
  41. ctx.beginPath();
  42. ctx.arc(corner2-x, corner2-y, 65, 0, 2*Math.PI);
  43. ctx.fill();
  44. //Zone Rendering Outer
  45. //Blue
  46. ctx.fillStyle = '#006480';
  47. ctx.beginPath();
  48. ctx.arc(corner1-x, corner1-y, 65*r, 0, 2*Math.PI);
  49. ctx.fill();
  50. //Purple
  51. ctx.fillStyle = '#E461F2';
  52. ctx.beginPath();
  53. ctx.arc(corner2-x, corner1-y, 65*r, 0, 2*Math.PI);
  54. ctx.fill();
  55. //Green
  56. ctx.fillStyle = '#00803e';
  57. ctx.beginPath;
  58. ctx.arc(corner1-x, corner2-y, 65*r, 0, 2*Math.PI);
  59. ctx.fill();
  60. //Red
  61. ctx.fillStyle = '#963033';
  62. ctx.beginPath();
  63. ctx.arc(corner2-x, corner2-y, 65*r, 0, 2*Math.PI);
  64. ctx.fill();
  65. //Zone Rendering Inner
  66. ctx.globalAlpha = 0.12;
  67. //Blue
  68. ctx.fillStyle = '#006480';
  69. ctx.beginPath();
  70. ctx.arc(corner1-x, corner1-y, 45*r, 0, 2*Math.PI);
  71. ctx.fill();
  72. //Purple
  73. ctx.fillStyle = '#E461F2';
  74. ctx.beginPath();
  75. ctx.arc(corner2-x, corner1-y, 45*r, 0, 2*Math.PI);
  76. ctx.fill();
  77. //Green
  78. ctx.fillStyle = '#00803e';
  79. ctx.beginPath;
  80. ctx.arc(corner1-x, corner2-y, 45*r, 0, 2*Math.PI);
  81. ctx.fill();
  82. //Red
  83. ctx.fillStyle = '#963033';
  84. ctx.beginPath();
  85. ctx.arc(corner2-x, corner2-y, 45*r, 0, 2*Math.PI);
  86. ctx.fill();
  87. }
  88. ctx.restore();
  89. }
  90. class Minimap {
  91. constructor() {
  92. this._minimapWidth;
  93. this._minimapHeight;
  94. this._x00;
  95. this._y00;
  96. this._pointX;
  97. this._pointY;
  98. this._pointX_previous;
  99. this._pointY_previous;
  100. this._viewportWidth;
  101. this._viewportHeight;
  102. this._fov;
  103.  
  104. this._minimapHook();
  105. this._arrowHook();
  106. this._viewportHook();
  107. this._fovHook();}
  108. get x() {
  109. return this._pointX ? (this._pointX - this._x00) / this._minimapWidth : 0;
  110. }
  111. get y() {
  112. return this._pointY ? (this._pointY - this._y00) / this._minimapHeight : 0;
  113. }
  114. get x_previous() {
  115. return this._pointX_previous ? (this._pointX_previous - this._x00) / this._minimapWidth : 0;
  116. }
  117. get y_previous() {
  118. return this._pointY_previous ? (this._pointY_previous - this._y00) / this._minimapHeight : 0;
  119. }
  120. get fov() {
  121. return this._fov;
  122. }
  123.  
  124. _minimapHook() {
  125. let setTransformArgs;
  126.  
  127. const onsetTransform = (args) => {
  128. if (args[0] === args[3]) setTransformArgs = args;
  129. };
  130. const onstrokeRect = () => {
  131. if (setTransformArgs) {
  132. this._minimapWidth = setTransformArgs[0];
  133. this._minimapHeight = setTransformArgs[3];
  134. this._x00 = setTransformArgs[4];
  135. this._y00 = setTransformArgs[5];
  136. setTransformArgs = undefined;
  137. }
  138. };
  139. this._ctxHook('setTransform', onsetTransform);
  140. this._ctxHook('strokeRect', onstrokeRect);
  141. }
  142. _arrowHook() {
  143. let index = 0;
  144. const stack = Array(4);
  145.  
  146. let pointA;
  147. let pointB;
  148. let pointC;
  149.  
  150. const calculatePos = () => {
  151. const side1 = Math.floor(
  152. Math.sqrt(Math.pow(pointA[0] - pointB[0], 2) + Math.pow(pointA[1] - pointB[1], 2))
  153. );
  154. const side2 = Math.floor(
  155. Math.sqrt(Math.pow(pointA[0] - pointC[0], 2) + Math.pow(pointA[1] - pointC[1], 2))
  156. );
  157. const side3 = Math.floor(
  158. Math.sqrt(Math.pow(pointB[0] - pointC[0], 2) + Math.pow(pointB[1] - pointC[1], 2))
  159. );
  160. if (side1 == side2 && side2 == side3) return;
  161.  
  162. this._pointX_previous = this._pointX;
  163. this._pointY_previous = this._pointY;
  164.  
  165. this._pointX = (pointA[0] + pointB[0] + pointC[0]) / 3;
  166. this._pointY = (pointA[1] + pointB[1] + pointC[1]) / 3;
  167. };
  168. const onbeginPath = () => {
  169. index = 0;
  170. stack[index++] = 0;
  171. };
  172. const onmoveTo = (args) => {
  173. if (index === 1 && stack[index - 1] === 0) {
  174. stack[index++] = 1;
  175. pointA = args;
  176. return;
  177. }
  178. index = 0;
  179. };
  180. const onlineTo = (args) => {
  181. if (index === 2 && stack[index - 1] === 1) {
  182. stack[index++] = 2;
  183. pointB = args;
  184. return;
  185. }
  186. if (index === 3 && stack[index - 1] === 2) {
  187. stack[index++] = 2;
  188. pointC = args;
  189. return;
  190. }
  191. index = 0;
  192. };
  193. const onfill = () => {
  194. if (index === 4 && stack[index - 1] === 2) {
  195. calculatePos();
  196. return;
  197. }
  198. index = 0;
  199. };
  200.  
  201. this._ctxHook('beginPath', onbeginPath);
  202. this._ctxHook('moveTo', onmoveTo);
  203. this._ctxHook('lineTo', onlineTo);
  204. this._ctxHook('fill', onfill);
  205. }
  206. _viewportHook() {
  207. let setTransformArgs;
  208.  
  209. const onsetTransform = (args) => {
  210. if ((args[0] / args[3]).toFixed(4) !== (unsafeWindow.innerWidth / unsafeWindow.innerHeight).toFixed(4)) return;
  211. if (args[0] >= unsafeWindow.innerWidth && args[3] >= unsafeWindow.innerHeight) return;
  212. setTransformArgs = args;
  213. };
  214. const onfillRect = () => {
  215. if (setTransformArgs) {
  216. unsafeWindow.input.set_convar('ren_minimap_viewport', true);
  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. setInterval(() => {
  227. unsafeWindow.input.set_convar('ren_minimap_viewport', true);
  228. }, 1000);
  229. }
  230. _fovHook() {
  231. let solid_background = false;
  232. setTimeout(() => {
  233. solid_background = unsafeWindow.input.get_convar('ren_solid_background') === 'true' ? true : false;
  234. }, 1000);
  235.  
  236. const calculateFov = (fov) => {
  237. this._fov = fov * 10;
  238. };
  239. function onstroke() {
  240. if (this.fillStyle === '#cdcdcd') {
  241. if (solid_background) unsafeWindow.input.set_convar('ren_solid_background', true);
  242. calculateFov(this.globalAlpha);
  243. }
  244. }
  245.  
  246. this._ctxHook('stroke', onstroke);
  247.  
  248. setInterval(() => {
  249. if (solid_background) unsafeWindow.input.set_convar('ren_solid_background', false);
  250. }, 10000);
  251. }
  252. _ctxHook(method, hook) {
  253. const target = window.CanvasRenderingContext2D.prototype;
  254. target[method] = new Proxy(target[method], {
  255. apply(target, thisArg, args) {
  256. args = hook.call(thisArg, args) || args;
  257. return target.apply(thisArg, args);
  258. },
  259. });
  260. }
  261. }
  262.  
  263. class Player {
  264. constructor() {
  265. this._minimap = new Minimap();
  266. this._dead = true;
  267.  
  268. //Dead Listener
  269. new MutationObserver((args) => {
  270. this._dead = args[0].target.style.display === 'block';
  271. if (this.ondead && this._dead) this.ondead();
  272. }).observe(document.getElementById('a'), { attributes: true });
  273. }
  274. get dead() {
  275. return this._dead;
  276. }
  277. get gamemode() {
  278. return unsafeWindow.localStorage.gamemode;
  279. }
  280. }
  281.  
  282. const player = new Player();
  283. const minimap = new Minimap();
  284. const width = 149;
  285. var arenaDim;
  286. var scale,a,x,y,d,compensation;
  287. var interX, interY, posX, posY, fov;
  288. //setup canvas
  289. const ctx = document.getElementById('canvas').getContext('2d');
  290. // run main Loop
  291.  
  292. unsafeWindow.requestAnimationFrame = new Proxy(unsafeWindow.requestAnimationFrame, {
  293. apply: function (target, thisArg, args) {
  294. x = minimap._pointX;
  295. d = minimap._minimapWidth;
  296. scale = d / 169;
  297. fov = player._minimap.fov / 0.55;
  298. arenaDim = 12480 * fov;
  299. y = minimap._pointY;
  300. compensation = a * 168.5;
  301. //Helpers
  302. if(window.innerHeight == screen.height) {
  303. a = 0;} else {a = 1;}
  304. interX = x - (1920-(1920-1722)*scale);
  305. interY = y - (1080-(1080-876)*scale-compensation);
  306. posX = interX / d * 12480 * fov - (1-fov)*946.5;
  307. posY = interY / d * 12480 * fov - (1-fov)*500.5;
  308. drawZones(posX, posY);
  309. setTimeout(() => Reflect.apply(target, thisArg, args), 0);
  310. },
  311. });