Slither.io bot

Bot for slither.io game

Tính đến 01-05-2016. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name Slither.io bot
// @include     http*
// @author       Creec Winceptor
// @description  Bot for slither.io game
// @run-at document-end
// @grant    GM_info
// @grant         unsafeWindow
// @version 1
// @namespace https://greatest.deepsurf.us/users/3167
// ==/UserScript==

//CODE STARTS HERE
if (window.top != window.self)  //don't run on frames or iframes
    return;


REALSCORE = 1;
REALDNA = [1,1,1,1,1,1,1,1,1,1,1];

var cleardna = function() {
	localStorage.removeItem("REALSCORE");
	localStorage.removeItem("REALDNA");
	REALSCORE = 10;
	REALDNA = [1.246,1.234,0.786,0.917,0.71,1.093,1.103,1.241,0.955,1.328,1.146];
	
	TESTDNA = REALDNA.slice();
	TESTSCORE = REALSCORE;
}

if(typeof(Storage) !== "undefined") {
    // Code for localStorage/sessionStorage.
	
	if (!localStorage.REALSCORE || !localStorage.REALDNA) {
		cleardna();
		localStorage["REALSCORE"] = REALSCORE;
		localStorage["REALDNA"] = JSON.stringify(REALDNA);
	}
	else
	{
		REALSCORE = localStorage["REALSCORE"]*1;
		REALDNA = JSON.parse(localStorage["REALDNA"]);
	}
} else {
    // Sorry! No Web Storage support..
}


TESTDNA = REALDNA.slice();
TESTSCORE = REALSCORE;

testingdna = 1;
DNA = REALDNA.slice();

var injected_dead = true;

var lastscore = 0;
var bestscore = 0;

var lastrank = 0;
var bestrank = 1000;
var uhka = false;


var ruokakerroin = 1/40;
var ruokapower = 1;

var viholliskerroin = 400;
var vihollispower = 2;

var vaarakerroin = 300;
var vaarapower = 2;

var preykerroin = 10;
var preypower = 1;

var pakoondistance = 150;
var chargedistance = 300;

var keskikerroin = 3;
var keskipower = 1;
var keskihakukerroin = 0.1;
var preychase = true;

var learnrate = 0.5;
var	precision = 100;


	
INJECTED = function() {
	var kerroin = 1000;
	if (animating) {
		if (snake) {
			accelerate = false;
			injected_dead = false;
			
			//xm = grd-snake.xx;
			//ym = grd-snake.yy;
			
			xt = 0;
			yt = 0;
			
			mindist = grd;
			
			
			for (var i = 0; i < preys.length; i++) {
				xtd = (preys[i].xx-snake.xx);
				ytd = (preys[i].yy-snake.yy);
				
				dist = Math.sqrt(xtd*xtd + ytd*ytd);
				
				xt += preykerroin*xtd/Math.pow(dist,preypower*DNA[0]+1)*DNA[1];
				yt += preykerroin*ytd/Math.pow(dist,preypower*DNA[0]+1)*DNA[1];
				
				if (dist < chargedistance && preychase)
				{
					accelerate = true;
				}
			}

			uhka = false;
			for (var i = 0; i < snakes.length; i++) {
				//alert(myStringArray[i]);
				
				target = snakes[i];
				
				if (snake.id!=target.id)
				{
					xtd = (target.xx-snake.xx);
					ytd = (target.yy-snake.yy);
					
					dist = Math.sqrt(xtd*xtd + ytd*ytd);
					//dist = Math.max(1, dist);
					
					xt += -viholliskerroin*xtd/Math.pow(dist,vihollispower*DNA[2]+1)*DNA[3];
					yt += -viholliskerroin*ytd/Math.pow(dist,vihollispower*DNA[2]+1)*DNA[3];
					
					
					
					if (dist<pakoondistance)
					{
						accelerate = true;
					}
					
					if (dist<mindist)
					{
						uhka = target;
						mindist = dist;
						/*if (mindist<300)
						{
							accelerate = true;
						}*/
					}
					
					parts = target.pts;
					for (var k = 0; k < parts.length; k++)
					{
						part = parts[k];
					
						xtd = (part.xx-snake.xx);
						ytd = (part.yy-snake.yy);
						
						dist = Math.sqrt(xtd*xtd + ytd*ytd);
						
						xt += -vaarakerroin*xtd/Math.pow(dist,vaarapower*DNA[4]+1)*DNA[5];
						yt += -vaarakerroin*ytd/Math.pow(dist,vaarapower*DNA[4]+1)*DNA[5];
					}
		
				}
			}
			
						
			if (!(preychase && preys.length>0))
			{
				for (var i = 0; i < foods_c; i++) {
					//alert(myStringArray[i]);
					
					xtd = (foods[i].xx-snake.xx);
					ytd = (foods[i].yy-snake.yy);
					
					dist = Math.sqrt(xtd*xtd + ytd*ytd);
					
					xt += ruokakerroin*xtd/Math.pow(dist,ruokapower*DNA[6]+1)*DNA[7]*foods[i].fw;
					yt += ruokakerroin*ytd/Math.pow(dist,ruokapower*DNA[6]+1)*DNA[7]*foods[i].fw;
					
				}
						
				xtd = (grd-snake.xx);
				ytd = (grd-snake.yy);
				
				dist = Math.sqrt(xtd*xtd + ytd*ytd);
				
				xt += keskikerroin*xtd/Math.pow(grd-dist,keskipower*DNA[8]+1)*DNA[9];
				yt += keskikerroin*ytd/Math.pow(grd-dist,keskipower*DNA[8]+1)*DNA[9];
				
				xt += keskihakukerroin*xtd/grd*DNA[10];
				yt += keskihakukerroin*ytd/grd*DNA[10];
				
				
			}
			
			xm = xt*kerroin;
			ym = yt*kerroin;
			
			lsxm = -xm;
			lsym = -ym;
			
			if (accelerate)
			{
				setAcceleration(1);
			}
			else
			{
				setAcceleration(0);
			}
			
			//console.log("xx:" +  view_xx + "yy:" + view_yy);
			//console.log("snake.xx:" +  snake.xx + "snake.yy:" + snake.yy);
			//console.log("snake.fx:" +  snake.fx + "snake.fy:" + snake.fy);
			//console.log("fvx:" +  fvx + "fvy:" + fvy);
			//console.log("");
			
			//console.log("foods_c:" + foods_c);
			//console.log("snakes.length:" + snakes.length);
			
			//console.log("D:" + Math.sqrt(xm*xm + ym*ym));
			
			lastscore = Math.floor(150 * (fpsls[snake.sct] + snake.fam / fmlts[snake.sct] - 1) - 50) / 10;
			lastrank = rank;
		}
		else
		{
			if (!injected_dead)
			{
				injected_dead = true;
				
				if (lastscore>bestscore)
				{
					console.log("Last score: " + lastscore + "(new best)");
					bestscore = lastscore;
				}
				else
				{
					console.log("Last score: " + lastscore + " Best: " + bestscore);
				}
				
				if (lastrank<bestrank)
				{
					console.log("Last rank: " + lastrank + "(new best)");
					bestrank = lastrank;
				}
				else
				{
					console.log("Last rank: " + lastrank + " Best: " + bestrank);
				}
				
				if (testingdna>0)
				{
					TESTSCORE = lastscore/lastrank;

				}
				else
				{
					REALSCORE = (REALSCORE+lastscore/lastrank)/2;
				}
				lastscore = 0;

				for (var key in DNA)
				{		
					if (TESTSCORE > REALSCORE)
					{
						var mul = TESTSCORE/REALSCORE
						REALDNA[key] = (REALDNA[key] + TESTDNA[key]*mul)/(1+mul);
						REALDNA[key] = Math.round(REALDNA[key]*precision)/precision;
					}

					if (testingdna<0)
					{
						TESTDNA[key] = REALDNA[key] + REALDNA[key]*(Math.random() - Math.random())*learnrate;
						TESTDNA[key] = Math.round(TESTDNA[key]*precision)/precision;
					}
				}
				
				localStorage["REALSCORE"] = REALSCORE;
				localStorage["REALDNA"] = JSON.stringify(REALDNA);

				testingdna = -testingdna;

				if (testingdna>0)
				{
					DNA = TESTDNA.slice();
				}
				else
				{
					DNA = REALDNA.slice();
				}
				//console.log("DNA = [" + DNA + "];");
				
				setTimeout(connect, 3000);
				//console.log("Reconnecting");
			}
		}
	}
}

var injectbot = function() {
	if (typeof(redraw) != "undefined")
	{
		oldredraw = redraw;

		redraw = function() {
			INJECTED();
			oldredraw();
		}
		console.log("injected")
		window.onmousemove = null;
	}
	else
	{
		setTimeout(injectbot, 1000);
		console.log("retrying")
	}
}
injectbot();