ermiyaeskandary / Slither.io-bot

Just for fun and AI. Written in Javascript, this is a project which the aim is to make a computer play against humans inside a human-driven game, which is in this case Slither.io. The goal is simple - try and make the snake live and get as long as possible.
Mozilla Public License 2.0
193 stars 124 forks source link

Lag reduce, running in worker thread #309

Open tungbuivn opened 8 years ago

tungbuivn commented 8 years ago

hello, first of all, i'm really like your superior script :) the lag happen when there are many snake on screen, and the loop handle too muck for ..loop, this cause client lag while running script, i'think you could put it into a worker thread, and your script only handle event from worker, i saw some where this script var blob = new Blob([ "onmessage = function(e) { postMessage('msg from worker'); }"]);

// Obtain a blob URL reference to our worker 'file'. var blobURL = window.URL.createObjectURL(blob);

var worker = new Worker(blobURL); worker.onmessage = function(e) { // e.data == 'msg from worker' }; worker.postMessage(); // Start the worker.

Doest is possible to make your script run in thread ?

and another issue is setTimeout,setInterval function, i think window.requestAnimationFrame is better, what do you think ? window.requestAnimFrame = (function(callback) { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / bot.opt.targetFps); }; })();

and a small piece of code check colision between snakes:

from line 617: canvasUtil.getDistance2FromSnake(collisionPoint); bot.addCollisionAngle(collisionPoint);

                        if (scPoint === undefined ||
                            scPoint.distance > collisionPoint.distance) {
                            scPoint = collisionPoint;
                        }

this is the colision of bone object, a safe colision will be side of bot snake face to, do i wrong ? if (scPoint === undefined || scPoint.distance > collisionPoint.distance-collisionPoint.radius-bot.snakeRadius) { scPoint = collisionPoint; }