Skip to content

Instantly share code, notes, and snippets.

@tobias93
Created October 28, 2016 12:29
Show Gist options
  • Save tobias93/3f9b516bc2e67eea1e0fa255cf99b824 to your computer and use it in GitHub Desktop.
Save tobias93/3f9b516bc2e67eea1e0fa255cf99b824 to your computer and use it in GitHub Desktop.
Simple Slither.io bot
// ==UserScript==
// @name Slither Bot
// @namespace http://slither.io/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://slither.io/
// @grant none
// ==/UserScript==
(function() {
'use strict';
var caref = 1;
setInterval(function(){
if (window.snake === null) {
//window.want_play=true;
return;
}
var sx = window.snake.xx;
var sy = window.snake.yy;
var attrack = window.foods.map(function(food){
if (food === null) return null;
var fx = food.xx;
var fy = food.yy;
var dx = fx-sx;
var dy = fy-sy;
var d = dx*dx+dy*dy;
return [dx, dy, d, food.sz];
}).reduce(function(a, b){
if (a === null) return b;
if (b === null) return a;
if (a[3] > 10 && b[3] < 10) return a;
if (a[3] < 10 && b[3] > 10) return b;
if(a[2] < b[2]) return a;
return b;
});
var go_away = window.snakes.map(function(sn){
if (sn === null) return [];
if (sn.xx === sx && sn.yy === sy) return [];
var ox = sn.xx;
var oy = sn.yy;
var dx = ox-sx;
var dy = oy-sy;
var d = dx*dx+dy*dy;
var ot = sn.pts.map(function (pt){
var dx = pt.xx-sx;
var dy = pt.yy-sy;
var d = dx*dx+dy*dy;
return [dx, dy, d/sn.sc/window.snake.sc];
});
ot.push([dx,dy,d/sn.sp*2/sn.sc/window.snake.sc]);
return ot;
}).reduce(function(a, b){
return a.concat(b);
}, []).reduce(function(a, b){
if (a === null) return b;
if (b === null) return a;
if(a[2] < b[2]) return a;
return b;
}, null);
console.log("go away: " + JSON.stringify(go_away));
console.log("attrack: " + JSON.stringify(attrack));
if (go_away !== null && go_away[2] < 5000 * caref * window.snake.sc) {
caref = 2;
window.xm = -go_away[0];
window.ym = -go_away[1];
console.log("I am careful");
} else if (attrack[2] > 5000) {
window.xm = attrack[0];
window.ym = attrack[1];
console.log("I am hungry");
caref = 1;
}
} ,100);
})();
@SOYUP
Copy link

SOYUP commented Jul 16, 2022

// ==UserScript==
// @name Slither Bot
// @namespace http://slither.io/
// @Version 0.1
// @description try to take over the world!
// @author You
// @match http://slither.io/
// @grant none
// ==/UserScript==

(function() {
'use strict';
var caref = 1;
setInterval(function(){
if (window.snake === null) {
//window.want_play=true;
return;
}
var sx = window.snake.xx;
var sy = window.snake.yy;
var attrack = window.foods.map(function(food){
if (food === null) return null;
var fx = food.xx;
var fy = food.yy;
var dx = fx-sx;
var dy = fy-sy;
var d = dxdx+dydy;
return [dx, dy, d, food.sz];
}).reduce(function(a, b){
if (a === null) return b;
if (b === null) return a;
if (a[3] > 10 && b[3] < 10) return a;
if (a[3] < 10 && b[3] > 10) return b;
if(a[2] < b[2]) return a;
return b;
});

    var go_away = window.snakes.map(function(sn){
        if (sn === null) return [];
        if (sn.xx === sx && sn.yy === sy) return [];
        var ox = sn.xx;
        var oy = sn.yy;
        var dx = ox-sx;
        var dy = oy-sy;
        var d = dx*dx+dy*dy;
        var ot = sn.pts.map(function (pt){
            var dx = pt.xx-sx;
            var dy = pt.yy-sy;
            var d = dx*dx+dy*dy;
            return [dx, dy, d/sn.sc/window.snake.sc];
        });
        ot.push([dx,dy,d/sn.sp*2/sn.sc/window.snake.sc]);
        return ot;
    }).reduce(function(a, b){
        return a.concat(b);
    }, []).reduce(function(a, b){
        if (a === null) return b;
        if (b === null) return a;
        if(a[2] < b[2]) return a;
        return b;
    }, null);
    
    console.log("go away: " + JSON.stringify(go_away));
    console.log("attrack: " + JSON.stringify(attrack));
    
    if (go_away !== null && go_away[2] < 5000 * caref * window.snake.sc) {
        caref = 2;
        window.xm = -go_away[0];
        window.ym = -go_away[1];
        console.log("I am careful");
    } else if (attrack[2] > 5000) {
        window.xm = attrack[0];
        window.ym = attrack[1];
        console.log("I am hungry");
        caref = 1;
    }
} ,100);

})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment