RicH HackS v3.0

SSlither.io Bot v1.2 - Slitheriogameplay.com

  1. // ==UserScript==
  2. // @name RicH HackS v3.0
  3. // @namespace http://slither.io/
  4. // @version 3.0
  5. // @description SSlither.io Bot v1.2 - Slitheriogameplay.com
  6. // @author Slitheriogameplay.com and Thanks to Ermiya Eskandary & Théophile Cailliau
  7. // @match http://slither.io/
  8. // @grant none
  9. // ==/UserScript==
  10. // Functions needed for the bot
  11. // Custom logging function - disabled by default
  12. window.log = function() {
  13. if (window.logDebugging) {
  14. console.log.apply(console, arguments);
  15. }
  16. };
  17. // Appends divs to the page - used to display things on the screen
  18. window.appendDiv = function(id, className, style) {
  19. // Create a div
  20. var div = document.createElement('div');
  21. // Check for id
  22. if (id) {
  23. // Set the id
  24. div.id = id;
  25. }
  26. // Check for class name
  27. if (className) {
  28. // Set the class name
  29. div.className = className;
  30. }
  31. // Check for css styles
  32. if (style) {
  33. // Set the css styles
  34. div.style = style;
  35. }
  36. // Append the div to the page
  37. document.body.appendChild(div);
  38. };
  39.  
  40. // Saves username when you click on "Play" button
  41. window.play_btn.btnf.addEventListener('click', function() {
  42. window.saveNick();
  43. });
  44. // Save nickname when you press "Enter"
  45. window.nick_holder.addEventListener('keypress', function(e) {
  46. if (e.keyCode == 13) {
  47. window.saveNick();
  48. }
  49. });
  50. // Save nickname
  51. window.saveNick = function() {
  52. var nick = document.getElementById('nick').value;
  53. window.savePreference('savedNick', nick);
  54. };
  55.  
  56. // Set fake mouse coordinates
  57. window.setMouseCoordinates = function(x, y) {
  58. window.xm = x;
  59. window.ym = y;
  60. };
  61. // Coordinates relative to the center (snake position).
  62. window.mouseRelativeToCenter = function(x, y) {
  63. var mapX = x - window.getWidth() / 2;
  64. var mapY = y - window.getHeight() / 2;
  65. return [mapX, mapY];
  66. };
  67. // Mouse coordinates to screen coordinates
  68. window.mouseToScreen = function(x, y) {
  69. var screenX = x + (window.getWidth() / 2);
  70. var screenY = y + (window.getHeight() / 2);
  71. return [screenX, screenY];
  72. };
  73. // Screen to canvas coordinates
  74. window.screenToCanvas = function(x, y) {
  75. var canvasX = window.csc * (x * window.canvasRatio[0]) - parseInt(window.mc.style.left);
  76. var canvasY = window.csc * (y * window.canvasRatio[1]) - parseInt(window.mc.style.top);
  77. return [canvasX, canvasY];
  78. };
  79. // Map to mouse coordinates
  80. window.mapToMouse = function(x, y) {
  81. var mouseX = (x - window.getX()) * window.gsc;
  82. var mouseY = (y - window.getY()) * window.gsc;
  83. return [mouseX, mouseY];
  84. };
  85. // Canvas width
  86. window.getWidth = function() {
  87. return window.ww;
  88. };
  89. // Canvas height
  90. window.getHeight = function() {
  91. return window.hh;
  92. };
  93. // X coordinates on the screen
  94. window.getX = function() {
  95. return window.snake.xx;
  96. };
  97. // Y coordinates on the screen
  98. window.getY = function() {
  99. return window.snake.yy;
  100. };
  101. // Updates the relation between the screen and the canvas
  102. window.onresize = function() {
  103. window.resize();
  104. // Canvas different size from the screen (often bigger). Gives a ratio so we can convert
  105. window.canvasRatio = [window.mc.width / window.getWidth(), window.mc.height / window.getHeight()];
  106. };
  107. // Lets you zoom in and out using the mouse wheel
  108. window.setZoom = function(e) {
  109. // Scaling ratio
  110. if (window.gsc) {
  111. window.gsc *= Math.pow(0.9, e.wheelDelta / -120 || e.detail / 2 || 0);
  112. }
  113. };
  114. // FPS counter
  115. window.framesPerSecond = {
  116. startTime: 0,
  117. frameNumber: 0,
  118. filterStrength: 40,
  119. lastLoop: 0,
  120. frameTime: 0,
  121. getFPS: function() {
  122. var thisLoop = performance.now();
  123. var thisFrameTime = thisLoop - this.lastLoop;
  124. this.frameTime += (thisFrameTime - this.frameTime) / this.filterStrength;
  125. this.lastLoop = thisLoop;
  126. return (1000 / this.frameTime).toFixed(0);
  127. }
  128. };
  129.  
  130. // Set background - default is slither.io's own background
  131. function setBackground(url = '/s/bg45.jpg') {
  132. window.ii.src = url;
  133. }
  134. // Reset zoom
  135. window.resetZoom = function() {
  136. window.gsc = 0.9;
  137. }
  138. // Get scaling ratio
  139. window.getScale = function() {
  140. return window.gsc;
  141. };
  142. // Snake length
  143. window.getSnakeLength = function() {
  144. return (Math.floor(150 * (window.fpsls[window.snake.sct] + window.snake.fam / window.fmlts[window.snake.sct] - 1) - 50) / 10);
  145. };
  146. // Save the original slither.io onmousemove function so we can re enable it back later
  147. window.mousemovelistener = window.onmousemove;
  148.  
  149. // Starts the bot
  150. window.launchBot = function() {
  151. window.log('Starting Bot.');
  152. window.isBotRunning = true;
  153. // Removed the onmousemove listener so we can move the snake manually by setting coordinates
  154. window.onmousemove = function() {};
  155. };
  156. // Stops the bot
  157. window.stopBot = function() {
  158. window.log('Stopping Bot.');
  159. // Re enable the original onmousemove function
  160. window.onmousemove = window.mousemovelistener;
  161. window.isBotRunning = false;
  162. // Clear the interval which starts the bot
  163. };
  164.  
  165. // Connects the bot
  166. window.connectBot = function() {
  167. if (!window.autoRespawn) return;
  168. // Stop the bot
  169. window.stopBot();
  170. window.log('Connecting...');
  171. // Connect the bot
  172. window.connect();
  173. // Check if bot can start
  174. window.botCanStart = setInterval(function() {
  175. if (window.playing) {
  176. window.launchBot();
  177. clearInterval(window.botCanStart);
  178. }
  179. }, 100);
  180. };
  181.  
  182. // Save variable to local storage
  183. window.savePreference = function(item, value) {
  184. window.localStorage.setItem(item, value);
  185. };
  186.  
  187. // Load a variable from local storage
  188. window.loadPreference = function(preference, defaultVar) {
  189. var savedItem = window.localStorage.getItem(preference);
  190. if (savedItem !== null) {
  191. if (savedItem == 'true') {
  192. window[preference] = true;
  193. } else if (savedItem == 'false') {
  194. window[preference] = false;
  195. } else {
  196. window[preference] = savedItem;
  197. }
  198. window.log('Setting found for ' + preference + ': ' + window[preference]);
  199. } else {
  200. window[preference] = defaultVar;
  201. window.log('No setting found for ' + preference + '. Used default: ' + window[preference]);
  202. }
  203. return window[preference];
  204. };
  205.  
  206. // Save the original slither.io onkeydown function so we can add stuff to it
  207. document.oldKeyDown = document.onkeydown;
  208. // Re write the function with our function
  209. document.onkeydown = function(e) {
  210. // Original slither.io onkeydown function + whatever is under it
  211. document.oldKeyDown(e);
  212. if (document.activeElement.parentElement !== window.nick_holder) {
  213. // Number `1` to toggle bot
  214. if (e.keyCode === 49) {
  215. if (window.isBotRunning) {
  216. window.stopBot();
  217. window.isBotEnabled = false;
  218. } else {
  219. window.launchBot(5);
  220. window.isBotEnabled = true;
  221. }
  222. }
  223. // Number '3' to toggle debugging (console)
  224. if (e.keyCode === 51) {
  225. window.logDebugging = !window.logDebugging;
  226. console.log('Log debugging set to: ' + window.logDebugging);
  227. window.savePreference('logDebugging', window.logDebugging);
  228. }
  229. // Number '2' to toggle debugging (visual)
  230. if (e.keyCode === 50) {
  231. window.visualDebugging = !window.visualDebugging;
  232. console.log('Visual debugging set to: ' + window.visualDebugging);
  233. window.savePreference('visualDebugging', window.visualDebugging);
  234. }
  235. // Number '4' to toggle autorespawn
  236. if (e.keyCode === 52) {
  237. window.autoRespawn = !window.autoRespawn;
  238. console.log('Automatic Respawning set to: ' + window.autoRespawn);
  239. window.savePreference('autoRespawn', window.autoRespawn);
  240. }
  241. // Number '5' to change rendermode (visual)
  242. if (e.keyCode === 53) {
  243. window.mobileRender = !window.mobileRender;
  244. console.log('Mobile rendering set to: ' + window.mobileRender);
  245. window.savePreference('mobileRender', window.mobileRender);
  246. // Set render mode
  247. if (window.mobileRender) {
  248. setBackground('data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs');
  249. window.render_mode = 1;
  250. } else {
  251. setBackground();
  252. window.render_mode = 2;
  253. }
  254. }
  255. // Number '6' to toggle hunting Prey
  256. if (e.keyCode === 54) {
  257. window.huntPrey = !window.huntPrey;
  258. console.log('Prey hunting set to: ' + window.huntPrey);
  259. window.savePreference('huntPrey', window.huntPrey);
  260. }
  261.  
  262. // Number '7' to toggle Collision detection / enemy avoidance
  263. if (e.keyCode === 55) {
  264. window.collisionDetection = !window.collisionDetection;
  265. console.log('collisionDetection set to: ' + window.collisionDetection);
  266. window.savePreference('collisionDetection', window.collisionDetection);
  267. }
  268.  
  269. // Letter 'A' to increase collision detection radius
  270. if (e.keyCode === 65) {
  271. window.collisionRadiusMultiplier++;
  272. console.log('collisionRadiusMultiplier set to: ' + window.collisionRadiusMultiplier);
  273. window.savePreference('collisionRadiusMultiplier', window.collisionRadiusMultiplier);
  274. }
  275.  
  276. // Letter 'S' to decrease collision detection radius
  277. if (e.keyCode === 83) {
  278. if (window.collisionRadiusMultiplier > 1) {
  279. window.collisionRadiusMultiplier--;
  280. console.log('collisionRadiusMultiplier set to: ' + window.collisionRadiusMultiplier);
  281. window.savePreference('collisionRadiusMultiplier', window.collisionRadiusMultiplier);
  282. }
  283. }
  284.  
  285. // Number '8' to toggle defence mode
  286. if (e.keyCode === 56) {
  287. window.defence = !window.defence;
  288. console.log('Defence set to: ' + window.defence);
  289. window.savePreference('defence', window.defence);
  290. }
  291. // Number '9' to reset zoom
  292. if (e.keyCode === 57) {
  293. window.resetZoom();
  294. }
  295. }
  296. };
  297. // Snake width
  298. window.getSnakeWidth = function() {
  299. return window.snake.sc * 15 * window.getScale();
  300. };
  301. // Sorting function for food, from property 'distance'
  302. window.sortFood = function(a, b) {
  303. // a.sz & b.sz - size
  304. // Divide distance by size so bigger food is prioritised over smaller food
  305. return a.distance / a.sz - b.distance / b.sz;
  306. };
  307. // Sorting function for prey, from property 'distance'
  308. window.sortPrey = function(a, b) {
  309. return a.distance - b.distance;
  310. };
  311.  
  312. // Convert object coordinates to radians
  313. window.getAngleFromObject = function(object) {
  314. var x = object.xx - window.getX();
  315. var y = object.yy - window.getY();
  316. return Math.atan2(x, y);
  317. };
  318.  
  319. // Polar angle to Cartesian angles
  320. window.getCoordsFromAngle = function(angle) {
  321. var x = Math.cos(angle) * 100;
  322. var y = Math.sin(angle) * 100;
  323. return [x, y];
  324. };
  325.  
  326. // Given an object (of which properties xx and yy are not null), return the object with an additional property 'distance'
  327. window.getDistanceFromMe = function(point) {
  328. if (point === null) return null;
  329. point.distance = window.getDistance(window.getX(), window.getY(), point.xx, point.yy);
  330. return point;
  331. };
  332. // Get a distance from point (x1; y1) to point (x2; y2).
  333. window.getDistance = function(x1, y1, x2, y2) {
  334. // Calculate the vector coordinates.
  335. var xDistance = (x1 - x2);
  336. var yDistance = (y1 - y2);
  337. // Get the absolute value of each coordinate
  338. xDistance = xDistance < 0 ? xDistance * -1 : xDistance;
  339. yDistance = yDistance < 0 ? yDistance * -1 : yDistance;
  340. //Add the coordinates of the vector to get a distance. Not the real distance, but reliable for distance comparison.
  341. var distance = xDistance + yDistance;
  342. // Real distance but not needed. Here for reference -
  343. // var distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
  344. return distance;
  345. };
  346. // Checks to see if you are going to collide with anything in the collision detection radius
  347. window.checkCollision = function(x, y, r) {
  348. var circle1 = collisionScreenToCanvas({
  349. x: x,
  350. y: y,
  351. radius: r
  352. });
  353. if (window.visualDebugging) {
  354. window.drawDot(circle1.x, circle1.y, circle1.radius, 'blue', false);
  355. }
  356. var avoid = false;
  357. var circle2;
  358.  
  359. for (var snake in window.snakes) {
  360. if (window.snakes[snake].nk != window.snake.nk) {
  361. for (var y = window.snakes[snake].pts.length - 1; 0 <= y; y--) {
  362. if (!window.snakes[snake].pts[y].dying) {
  363. circle2 = {
  364. x: window.snakes[snake].pts[y].xx + window.snakes[snake].fx,
  365. y: window.snakes[snake].pts[y].yy + window.snakes[snake].fy,
  366. radius: 15 * window.snakes[snake].sc * window.getScale()
  367. };
  368. if (window.circleIntersect(circle1, collisionScreenToCanvas(circle2))) {
  369. window.changeGoalCoords(circle2);
  370. avoid = true;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. return avoid;
  377. };
  378. // Screen to Canvas coordinate conversion - used for collision detection
  379. window.collisionScreenToCanvas = function(circle) {
  380. var newCircle = window.mapToMouse(circle.x, circle.y);
  381. newCircle = window.mouseToScreen(newCircle[0], newCircle[1]);
  382. newCircle = window.screenToCanvas(newCircle[0], newCircle[1]);
  383.  
  384. return {
  385. x: newCircle[0],
  386. y: newCircle[1],
  387. radius: circle.radius
  388. };
  389. };
  390. // Change direction
  391. window.changeGoalCoords = function(circle1) {
  392. if ((circle1.x != window.collisionPoint.x && circle1.y != window.collisionPoint.y)) {
  393. window.collisionPoint = circle1;
  394. window.goalCoordinates = window.mapToMouse(window.snake.xx + (window.snake.xx - window.collisionPoint.x), window.snake.yy + (window.snake.yy - window.collisionPoint.y));
  395. window.setAcceleration(0);
  396. window.setMouseCoordinates(goalCoordinates[0], goalCoordinates[1]);
  397. }
  398. };
  399. // Check if circles intersect
  400. window.circleIntersect = function(circle1, circle2) {
  401. if (quickCollisionCheck(circle1, circle2)) {
  402. if (collisionCheck(circle1, circle2)) {
  403. return true;
  404. } else {
  405. return false;
  406. }
  407. } else {
  408. return false;
  409. }
  410. };
  411. // Quickly check if we are going to collide with anything
  412. window.quickCollisionCheck = function(circle1, circle2) {
  413. return (circle1.x + circle1.radius + circle2.radius > circle2.x &&
  414. circle1.x < circle2.x + circle1.radius + circle2.radius &&
  415. circle1.y + circle1.radius + circle2.radius > circle2.y &&
  416. circle1.y < circle2.y + circle1.radius + circle2.radius);
  417. };
  418. // Collision check
  419. window.collisionCheck = function(circle1, circle2) {
  420. distance = Math.sqrt(((circle1.x - circle2.x) * (circle1.x - circle2.x)) + ((circle1.y - circle2.y) * (circle1.y - circle2.y)));
  421.  
  422. if (distance < circle1.radius + circle2.radius) {
  423. collisionPointX = ((circle1.x * circle2.radius) + (circle2.x * circle1.radius)) / (circle1.radius + circle2.radius);
  424. collisionPointY = ((circle1.y * circle2.radius) + (circle2.y * circle1.radius)) / (circle1.radius + circle2.radius);
  425.  
  426. if (window.visualDebugging) {
  427. window.drawDot(collisionPointX, collisionPointY, circle2.radius, 'green', true);
  428. window.drawDot(circle2.x, circle2.y, circle2.radius, 'blue', true);
  429. }
  430. return true;
  431. } else {
  432. return false;
  433. }
  434. };
  435.  
  436. // Sort food based on distance
  437. window.getSortedFood = function() {
  438. // Filters the nearest food by getting the distance
  439. return window.foods.filter(function(val) {
  440. return val !== null;
  441. }).map(window.getDistanceFromMe).sort(window.sortFood);
  442. };
  443. // Sort prey based on distance
  444. window.getSortedPrey = function() {
  445. // Filters the nearest food by getting the distance
  446. return window.preys.filter(function(val) {
  447. return val !== null;
  448. }).map(window.getDistanceFromMe).sort(window.sortPrey);
  449. };
  450. // Draw dots on the canvas
  451. window.drawDot = function(x, y, radius, colour, fill) {
  452. var context = window.mc.getContext('2d');
  453. context.beginPath();
  454. context.strokeStyle = '#00FF00';
  455. context.arc(x, y, radius * window.getScale(), 0, Math.PI * 2);
  456. context.closePath();
  457. if (fill) {
  458. context.fillStyle = ('green red white yellow black cyan blue'.indexOf(colour) < 0) ? 'white' : colour;
  459. context.fill();
  460. context.fillStyle = 'black';
  461. }
  462. };
  463.  
  464. // Draw lines on the canvas
  465. window.drawLine = function(x2, y2, colour) {
  466. var context = window.mc.getContext('2d');
  467. var center = [window.mc.height / 2, window.mc.width / 2];
  468. context.lineWidth = 5 * window.getScale();
  469. context.strokeStyle = (colour === 'blue') ? '#0000FF' : '#FF0000';
  470. context.moveTo(center[1], center[0]);
  471. context.lineTo(x2, y2);
  472. context.stroke();
  473. context.strokeStyle = '#000000';
  474. };
  475. // Save the original slither.io oef function so we can add things to it later
  476. window.oldOef = window.oef;
  477. window.oef = function() {
  478. // Original slither.io oef function + whatever is under it
  479. // requestAnimationFrame(window.loop);
  480. window.oldOef();
  481. if (window.isBotRunning) window.loop();
  482. window.onFrameUpdate();
  483. };
  484. window.handleTextColor = function(enabled) {
  485. return '<span style=\"opacity: 0.8; color:' + (enabled ? 'green;\"> ►ON- RandomModzzz.weebly.com' : 'red;\"> ►OFF- RandomModzzz.weebly.com') + '</span>';
  486. };
  487. window.onFrameUpdate = function() {
  488. // Botstatus overlay
  489. var generalStyle = '<span style = "opacity: 0.35";>';
  490. window.botstatus_overlay.innerHTML = generalStyle + '_______________ PRESS (1) TO TOGGLE: Auto Bot: </span>' + window.handleTextColor(window.isBotRunning);
  491. window.visualdebugging_overlay.innerHTML = generalStyle + '|RicH HackS v3.0 | PRESS (2) TO TOGGLE: Visual Target Finder: </span>' + window.handleTextColor(window.visualDebugging);
  492. window.logdebugging_overlay.innerHTML = generalStyle + '|______________| PRESS (3) TO TOGGLE: Log debugging: </span>' + window.handleTextColor(window.logDebugging);
  493. window.autorespawn_overlay.innerHTML = generalStyle + ' PRESS (4) TO TOGGLE: Auto respawn: </span>' + window.handleTextColor(window.autoRespawn);
  494. window.rendermode_overlay.innerHTML = generalStyle + ' PRESS (5) TO TOGGLE: Mobile rendering: </span>' + window.handleTextColor(window.mobileRender);
  495. window.huntprey_overlay.innerHTML = generalStyle + ' PRESS (6) TO TOGGLE: Pretator v1.27: </span>' + window.handleTextColor(window.huntPrey);
  496. window.collision_detection_overlay.innerHTML = generalStyle + ' PRESS (7) TO TOGGLE: Collision detection: </span>' + window.handleTextColor(window.collisionDetection);
  497. window.collision_radius_multiplier_overlay.innerHTML = generalStyle + ' PRESS (A/S) TO TOGGLE: Collision radius multiplier: ' + window.collisionRadiusMultiplier + ' </span>';
  498. window.defence_overlay.innerHTML = generalStyle + ' PRESS (8) TO TOGGLE: Circle Mode: </span>' + window.handleTextColor(window.defence);
  499. window.resetzoom_overlay.innerHTML = generalStyle + ' PRESS(9) TO TOGGLE: Reset zoom </span>';
  500. window.fps_overlay.innerHTML = generalStyle + 'RicH HackS v3.0 Speed Xmade by griffinX: ' + window.framesPerSecond.getFPS() + '</span>';
  501.  
  502. // If playing
  503. if (window.playing && window.visualDebugging) {
  504. if (window.isBotRunning) {
  505. // Check to see if there is a position overlay
  506. if (window.position_overlay) {
  507. // Display the X and Y of the snake
  508. window.position_overlay.innerHTML = generalStyle + 'X: ' + (Math.round(window.snake.xx) || 0) + ' Y: ' + (Math.round(window.snake.yy) || 0) + '</span>';
  509. }
  510. drawGoalCoordinates = window.mouseToScreen(window.goalCoordinates[0], window.goalCoordinates[1]);
  511. drawGoalCoordinates = window.screenToCanvas(drawGoalCoordinates[0], drawGoalCoordinates[1]);
  512. window.drawLine(drawGoalCoordinates[0], drawGoalCoordinates[1], 'green');
  513. window.drawDot(drawGoalCoordinates[0], drawGoalCoordinates[1], 5, 'red', true);
  514. }
  515. }
  516. };
  517. // Defense mode - bot turns around in a circle
  518. window.playDefence = function(dir) {
  519. window.kd_l = (dir === "l");
  520. window.kd_r = (dir === "r");
  521. window.setMouseCoordinates(window.getWidth() / 2, window.getHeight() / 2);
  522. };
  523. // Actual bot code
  524.  
  525. // Loop for running the bot
  526. window.loop = function() {
  527. // If the game and the bot are running
  528. if (window.playing && window.isBotEnabled) {
  529. window.ranOnce = true;
  530. // TODO: Check some condition to see if we should play defence
  531. // Right now this just uses the manual toggle
  532. if (window.defence) {
  533. window.playDefence("l");
  534. return;
  535. }
  536. // If no enemies or obstacles, go after what you are going after
  537. if (!window.checkCollision(window.getX(), window.getY(), window.getSnakeWidth()*window.collisionRadiusMultiplier)) {
  538. // Sort the food based on their distance relative to player's snake
  539. window.sortedFood = window.getSortedFood();
  540. // Current food
  541. window.currentFood = window.sortedFood[0];
  542. // Convert coordinates of the closest food using mapToMouse
  543. var coordinatesOfClosestFood = window.mapToMouse(window.currentFood.xx, window.currentFood.yy);
  544. window.goalCoordinates = coordinatesOfClosestFood;
  545. // Disable Sprint
  546. window.setAcceleration(0);
  547. // Check for preys, enough "length"
  548. if (window.preys.length > 0 && window.huntPrey) {
  549. // Sort preys based on their distance relative to player's snake
  550. window.sortedPrey = window.getSortedPrey();
  551. // Current prey
  552. window.currentPrey = window.sortedPrey[0];
  553. // Convert coordinates of the closest prey using mapToMouse
  554. var coordinatesOfClosestPrey = window.mapToMouse(window.currentPrey.xx, window.currentPrey.yy);
  555. // Check for the distance
  556. if (window.currentPrey.distance <= Math.pow(window.getSnakeLength(), 2) / 2) {
  557. // Set the mouse coordinates to the coordinates of the closest prey
  558. window.goalCoordinates = coordinatesOfClosestPrey;
  559. // "Sprint" enabled
  560. window.setAcceleration(1);
  561. }
  562. }
  563. window.kd_l = false;
  564. window.kd_r = false;
  565. window.setMouseCoordinates(window.goalCoordinates[0], window.goalCoordinates[1]);
  566. }
  567. } else {
  568. if (window.ranOnce) {
  569. //window.startInterval = setInterval(window.startBot, 1000);
  570. window.stopBot();
  571. }
  572. }
  573. };
  574.  
  575. // Target the user's browser.
  576. (function() {
  577. var requestAnimationFrame = window.requestAnimationFrame ||
  578. window.mozRequestAnimationFrame ||
  579. window.webkitRequestAnimationFrame ||
  580. window.msRequestAnimationFrame;
  581.  
  582. window.requestAnimationFrame = requestAnimationFrame;
  583. })();
  584.  
  585. // Starts bot
  586. window.startBot = function() {
  587. if (window.autoRespawn && !window.playing && window.isBotEnabled && window.ranOnce && !window.isBotRunning) {
  588. window.connectBot();
  589. //clearInterval(window.startInterval);
  590. }
  591. };
  592. // Initialises the bot
  593. window.initBot = function() {
  594. window.ranOnce = false;
  595. window.isBotRunning = false;
  596. window.isBotEnabled = true;
  597. window.collisionPoint = {
  598. x: 0,
  599. y: 0,
  600. radius: 0
  601. };
  602. // Load preferences
  603. window.loadPreference('logDebugging', false);
  604. window.loadPreference('visualDebugging', false);
  605. window.loadPreference('autoRespawn', false);
  606. window.loadPreference('mobileRender', false);
  607. window.loadPreference('huntPrey', true);
  608. window.loadPreference('collisionDetection', true);
  609. window.loadPreference('collisionRadiusMultiplier', 8);
  610. window.loadPreference('defence', false);
  611. window.nick.value = window.loadPreference('savedNick', 'Slitheriogameplay.com Bot!');
  612. // Overlays
  613. // Top left
  614. window.generalstyle = 'color: #FFF; font-family: Arial, \'Helvetica Neue\', Helvetica, sans-serif; font-size: 14px; position: fixed; z-index: 7;';
  615. window.appendDiv('botstatus_overlay', 'nsi', window.generalstyle + 'left: 30; top: 30px;');
  616. window.appendDiv('visualdebugging_overlay', 'nsi', window.generalstyle + 'left: 30; top: 45px;');
  617. window.appendDiv('logdebugging_overlay', 'nsi', window.generalstyle + 'left: 30; top: 60px;');
  618. window.appendDiv('autorespawn_overlay', 'nsi', window.generalstyle + 'left: 30; top: 75px;');
  619. window.appendDiv('rendermode_overlay', 'nsi', window.generalstyle + 'left: 30; top: 90px;');
  620. window.appendDiv('huntprey_overlay', 'nsi', window.generalstyle + 'left: 30; top: 105px;');
  621. window.appendDiv('collision_detection_overlay', 'nsi', window.generalstyle + 'left: 30; top: 120px;');
  622. window.appendDiv('collision_radius_multiplier_overlay', 'nsi', window.generalstyle + 'left: 30; top: 135px;');
  623. window.appendDiv('defence_overlay', 'nsi', window.generalstyle + 'left: 30; top: 150px;');
  624. window.appendDiv('resetzoom_overlay', 'nsi', window.generalstyle + 'left: 30; top: 165px;');
  625. // Bottom right
  626. window.appendDiv('position_overlay', 'nsi', window.generalstyle + 'right: 30; bottom: 120px;');
  627. window.appendDiv('fps_overlay', 'nsi', window.generalstyle + 'right: 30; bottom: 170px;');
  628. // Listener for mouse wheel scroll - used for setZoom function
  629. document.body.addEventListener('mousewheel', window.setZoom);
  630. document.body.addEventListener('DOMMouseScroll', window.setZoom);
  631. // Set render mode
  632. if (window.mobileRender) {
  633. setBackground('data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs');
  634. window.render_mode = 1;
  635. } else {
  636. setBackground();
  637. window.render_mode = 2;
  638. }
  639. // Canvas Ratio
  640. window.canvasRatio = [window.mc.width / window.getWidth(), window.mc.height / window.getHeight()];
  641. // Unblocks all skins without the need for FB sharing.
  642. window.localStorage.setItem('edttsg', '1');
  643. // Remove social
  644. window.social.remove();
  645. // Start!
  646. window.launchBot(50);
  647. window.startInterval = setInterval(window.startBot, 1000);
  648. };
  649. window.initBot();
  650.  
  651. // Enemy code - not used for now
  652. /*
  653. // Sort enemies based on distance
  654. window.getSortedEnemies = function() {
  655. Filters the nearest food by getting the distance
  656. return window.snakes.filter(function(val) {
  657. return val !== null && val.id !== window.snake.id;
  658. }).map(window.getDistanceFromMe).sort(window.sortEnemy);
  659. };
  660. // Sorting function for enemies, from property 'distance'
  661. window.sortEnemy = function(a, b) {
  662. return a.distance - b.distance;
  663. };
  664. window.sortedEnemies = window.getSortedEnemies();
  665. // Take the closest of each
  666. window.closestEnemy = window.sortedEnemies[0];
  667. if (window.closestEnemy.distance < 300) {
  668. window.log('close enemy! (distance = ' + window.closestEnemy.distance);
  669. // !handle close enemies!
  670. }
  671. */
  672. // Better food hunting algorithm but not implemented yet
  673. /*
  674. window.isInFoods = function (foodObject) {
  675. return (foodObject === null) ? false : (window.foods.indexOf(foodObject) >= 0);
  676. };
  677. window.currentFood = null;
  678. window.sortedFood = getSortedFood();
  679. window.loop = function () {
  680. if (!isInFoods(currentFood)) {
  681. window.sortedFood = getSortedFood();
  682. window.currentFood = sortedFood[0];
  683. var coordinatesOfClosestFood = window.mapToMouse(window.sortedFood[0].xx, window.sortedFood[0].yy);
  684. window.setMouseCoordinates(coordinatesOfClosestFood[0], coordinatesOfClosestFood[1]);
  685. }
  686. };
  687. */