Diep.io Minimap AFK

Moves back to original position when someone bumps you

  1. // ==UserScript==
  2. // @name Diep.io Minimap AFK
  3. // @namespace https://diep.io/*
  4. // @version 1.01
  5. // @description Moves back to original position when someone bumps you
  6. // @author Binary
  7. // @match https://diep.io/*
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. /// UI ///
  13.  
  14. var selflocation = [0, 0];
  15. var homebaselocation = [0, 0];
  16. var debug = false;
  17. var isleader = false;
  18. var afk = false;
  19. var acceptable_distance = 2;
  20.  
  21. var overlay_elements = {};
  22. var singular_keydown_events = {};
  23.  
  24. var overlay = document.createElement('div');
  25. overlay.style.position = 'fixed';
  26. overlay.style.top = '20px';
  27. overlay.style.left = '30px';
  28. overlay.style.fontFamily = 'Lucida Console, Courier, monospace';
  29. overlay.style.fontSize = '12px';
  30. overlay.style.color = '#ffffff';
  31. overlay.style.pointerEvents = 'none';
  32. overlay.style.userSelect = 'none';
  33. document.body.appendChild(overlay);
  34.  
  35. document.body.addEventListener('keydown', function (event) {
  36. if (!event.ctrlKey && !event.shiftKey && !event.altKey && !event.repeat) {
  37. if (singular_keydown_events[event.code]) singular_keydown_events[event.code]();
  38. }
  39. });
  40.  
  41. function addToOverlay(value_name, value_key = 'default') {
  42. var wrap_element = document.createElement('p');
  43. wrap_element.textContent = value_name + (value_key === 'default' ? '' : ': ');
  44. overlay_elements[value_key] = document.createElement('span');
  45. wrap_element.appendChild(overlay_elements[value_key]);
  46. overlay.appendChild(wrap_element);
  47. }
  48. addToOverlay('Diep.io Minimap AFK');
  49. addToOverlay('Press / to show/hide this overlay');
  50. addToOverlay('X-Axis', 'location_x');
  51. addToOverlay('Y-Axis', 'location_y');
  52. addToOverlay('Press q to enable AFK', 'afk_boolean');
  53. addToOverlay('Press j to save AFK location', 'home_base_location');
  54. addToOverlay('Press , to change AFK radius', 'acceptable_distance');
  55. addToOverlay('Press [ if you are a leader (IMPORTANT)', 'isleader_boolean');
  56. addToOverlay('Press r to enable debug (attempts to turn minimap triangle to white)', 'debug_boolean');
  57.  
  58. var show_overlay = false;
  59. overlay.style.display = 'none';
  60. singular_keydown_events['Slash'] = function () {
  61. overlay.style.display = (show_overlay = !show_overlay) ? '' : 'none';
  62. };
  63.  
  64. overlay_elements['afk_boolean'].textContent = afk;
  65. singular_keydown_events['KeyQ'] = function() {
  66. overlay_elements['afk_boolean'].textContent = (afk = !afk);
  67. };
  68.  
  69. overlay_elements['home_base_location'].textContent = JSON.stringify(homebaselocation);
  70. singular_keydown_events['KeyJ'] = function() {
  71. homebaselocation[0] = selflocation[0];
  72. homebaselocation[1] = selflocation[1];
  73. overlay_elements['home_base_location'].textContent = JSON.stringify(homebaselocation);
  74. };
  75.  
  76. overlay_elements['acceptable_distance'].textContent = acceptable_distance;
  77. singular_keydown_events['Comma'] = function() {
  78. overlay_elements['acceptable_distance'].textContent = (acceptable_distance = parseFloat(prompt('Change acceptable distance to...', acceptable_distance)));
  79. };
  80.  
  81. overlay_elements['isleader_boolean'].textContent = isleader;
  82. singular_keydown_events['BracketLeft'] = function() {
  83. overlay_elements['isleader_boolean'].textContent = (isleader = !isleader);
  84. };
  85.  
  86. overlay_elements['debug_boolean'].textContent = debug;
  87. singular_keydown_events['KeyR'] = function() {
  88. overlay_elements['debug_boolean'].textContent = (debug = !debug);
  89. };
  90.  
  91. /// GET SELF LOCATION ///
  92.  
  93. var position = 0;
  94. var position2 = 0;
  95. var original_getContext = HTMLCanvasElement.prototype.getContext;
  96. HTMLCanvasElement.prototype.getContext = function(...args){
  97. var context = original_getContext.apply(this, args);
  98.  
  99. // I have included these wrapping functions for y'all programmers to play around with (it's kinda fun)
  100. var wrapFunc = function(targetproperty, wrapfunction){
  101. var property = context[targetproperty];
  102. context[targetproperty] = function(...args){
  103. if(wrapfunction(args)) return;
  104. property.apply(context, args);
  105. };
  106. };
  107. var wrapSetter = function(targetproperty, wrapfunction){
  108. var setter = context.__lookupSetter__(targetproperty);
  109. context.__defineSetter__(targetproperty, function(newvalue){
  110. var callbackResult = wrapfunction(newvalue);
  111. if(callbackResult === true) return;
  112. setter.call(context, callbackResult || newvalue);
  113. });
  114. };
  115. wrapFunc('strokeRect', function(){
  116. position = 0;
  117. });
  118. wrapFunc('moveTo', function(args){
  119. if(position2++ === 0) {
  120. selflocation = args;
  121. overlay_elements['location_x'].textContent = selflocation[0];
  122. overlay_elements['location_y'].textContent = selflocation[1];
  123. }
  124. });
  125.  
  126. wrapSetter('fillStyle', function(newValue){
  127. if(newValue === 'rgb(0,0,0)' && position++ === (isleader ? 1 : 2)){
  128. position2 = 0;
  129. if(debug) return 'rgb(255,255,255)';
  130. }
  131. });
  132.  
  133. return context;
  134. };
  135.  
  136. /// MOVE ALGORITHM ///
  137.  
  138. var movement_control_keys = {
  139. left: 37,
  140. up: 38,
  141. right: 39,
  142. down: 40
  143. };
  144. var control_order = [
  145. function(){press(movement_control_keys.up)},
  146. function(){press(movement_control_keys.up);press(movement_control_keys.right)},
  147. function(){press(movement_control_keys.right)},
  148. function(){press(movement_control_keys.right);press(movement_control_keys.down)},
  149. function(){press(movement_control_keys.down)},
  150. function(){press(movement_control_keys.down);press(movement_control_keys.left)},
  151. function(){press(movement_control_keys.left)},
  152. function(){press(movement_control_keys.left);press(movement_control_keys.up)},
  153. function(){press(movement_control_keys.up)},
  154. ];
  155. function getBearing(selfx, selfy, targetx, targety) {
  156. // flip y axis for canvas's weird non-cartesian plane;
  157. selfy *= -1;
  158. targety *= -1;
  159.  
  160. var bearing = Math.PI - Math.atan2(targety - selfy, targetx - selfx);
  161. bearing -= Math.PI / 2;
  162. if (bearing < 0) bearing += 2 * Math.PI;
  163. return bearing;
  164. }
  165. function getDistance(selfx, selfy, targetx, targety) {
  166. return Math.sqrt(Math.pow(selfx - targetx, 2) + Math.pow(selfy - targety, 2));
  167. }
  168. function flushInputs(){
  169. for(var eachKey in movement_control_keys){
  170. unsafeWindow.input.keyUp(movement_control_keys[eachKey]);
  171. }
  172. }
  173. function press(key){
  174. unsafeWindow.input.keyDown(key);
  175. }
  176.  
  177. var clock = 1; // 1 to 10
  178. var end_tick = 10;
  179. var tick_time = 60; // end_tick * tick_time = total time for one cycle
  180. var previousafk = false; // flush keydown events when afk is turned off
  181. var gotolocation = function () {
  182. if (afk) {
  183. previousafk = true;
  184. flushInputs();
  185.  
  186. var bearing = getBearing(selflocation[0], selflocation[1], homebaselocation[0], homebaselocation[1]);
  187. var distance = getDistance(selflocation[0], selflocation[1], homebaselocation[0], homebaselocation[1]);
  188. if (acceptable_distance < distance) {
  189. var key_sections = bearing / (2 * Math.PI); // normalize bearing
  190. var time_section = 0; // portion of one cycle allocated to the first key
  191. var first_key = function(){};
  192. var second_key = function(){};
  193.  
  194. for (let section = 1; section <= 8; section++) {
  195. if (key_sections < section / 8) {
  196. time_section = (key_sections - ((section - 1) / 8)) * 8;
  197. first_key = control_order[section - 1];
  198. second_key = control_order[section];
  199. break;
  200. }
  201. }
  202. if ((clock++ / end_tick) > time_section) {
  203. first_key();
  204. } else {
  205. second_key();
  206. }
  207.  
  208. if (clock > end_tick) clock = 1;
  209. } else {
  210. clock = 1;
  211. }
  212. }else{
  213. if(previousafk){
  214. flushInputs();
  215. previousafk = false;
  216. }
  217. }
  218. setTimeout(gotolocation, tick_time);
  219. };
  220. gotolocation();
  221. })();