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
195 stars 124 forks source link

Javascript is bottleneck #101

Open ksofiyuk opened 8 years ago

ksofiyuk commented 8 years ago

I think if we want to create a really good bot need to use general purpose programming languages. I have some complex algorithmic solution for slither, but it seems impossible to implement it using Javascript with realtime performance.

Is there a simple way to connect the game interface with external libraries that are written in Python or C++?

P.S. I'm new in Javascript, but have a lot of experience with C++ and Python programming.

ermiyaeskandary commented 8 years ago

We could use custom created client programs to connect to the server....

have a lot of experience with C++ and Python programming.

P.S: same here... JavaScript seems like a snail compared to C++ ..

ermiyaeskandary commented 8 years ago

There are no existing clients written in Python or C++ or anything like that matter. Either JavaScript or HTML... :( Unless you want to make one ...

K00sKlust commented 8 years ago

Javascript is bottleneck

It is. Especially because it allows just one thread, which is also needed for the slither.io calculations. And I got more experience with other languages too..

A custom client program would be pretty nice :)) 👍

ermiyaeskandary commented 8 years ago

Nice but hard :)

Fredyy90 commented 8 years ago

Isn't there a way to build a small programm with a webview running slither.io and bury all logic in c/c++ and only use a small js interface?

ermiyaeskandary commented 8 years ago

Accident - sorry

ermiyaeskandary commented 8 years ago

@Fredyy90 It would be possible but we would still be communicating using JavaScript. Are you saying the back end would be ... And then the frontend would be js ?

Fredyy90 commented 8 years ago

I guess we only need Js in this setup to get the data from slither.io out, to process it in c/c++ and a way to return calculated the output stuff, like mouse coordinates, speedboost etc.

mirorauhala commented 8 years ago

Here is a crazy idea (because I do not know C++).

A quick Googling gave me these links: https://discuss.atom.io/t/electron-and-c-dll/17615/2 https://nodejs.org/api/addons.html#addons_hello_world

Now here's how I see it working: Scenario 1 Make a desktop application (with ElectronJS) that is a WebView to slither.io and the user will have to use the app to play the game with the bot.

Scenario 2 An ElectronJS app on users computer will act as a WebSocket server that the browser extension can then talk to. This means that the user doesn't have to use a different application to play the game, but still requires installing an app for the complex calculations at back-end.

ermiyaeskandary commented 8 years ago

@FliiFe Is'nt that what you did for Apos's bot ?

FliiFe commented 8 years ago

@ErmiyaEskandary It's kinda. But the backend was just sending a list of scripts to load. Implementing script in a backend is, for sure, a good idea, but that would require a lot of work.

ermiyaeskandary commented 8 years ago

Agreed.

ChadSki commented 8 years ago

@mirorauhala good suggestions. I would add:

Scenario 3 Use asm.js to compile C++ (or other) down to fast javascript.

All of these options are interesting but complicated.

ChadSki commented 8 years ago

I found this StackOverflow discussion about using node-webkit (now "NW.js") to modify a website like Greasemonkey / Tampermonkey does.

With NW.js, the client-side and server-side are the same thing, so our AI could use node.js features. This also provides indirect access to C++.

ChadSki commented 8 years ago

Experimental SIMD technology might be useful. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SIMD

The bonus of SIMD is that it's not terribly complicated to use -- it would still work in the browser.

tl;dr it lets you do more math at the same time.

Four math operations: SISD math

One math operation: SIMD math

FliiFe commented 8 years ago

@ChadSki Great idea, but it looks like we will have to wait a bit... capture du 2016-05-15 12-03-25

FliiFe commented 8 years ago

@ChadSki Oh, and about asm, that wouldn't work since we access game variables and we modify them.

ChadSki commented 8 years ago

asm wouldn't work since we access game variables and we modify them.

Why wouldn't it work? asm.js code can fully interop with normal javascript.

Example interop between javascript and C++ (compiled to asm.js with emscripten).

ChadSki commented 8 years ago

This looks handy for the asm.js route: https://github.com/gasman/bonsai-c

bonsai-c is a tool for writing programs, not porting them. By building a C compiler from the ground up, rather than bolting a Javascript backend onto a mature C compiler, we can tailor it to the needs of Javascript programmers. You can use it to write a function for calculating fibonacci numbers, and you will get back an asm.js-compliant Javascript module with an obvious entry point, for calculating fibonacci numbers. You will not get a mountain of runtime code to implement file handling and malloc and a million other things you didn't ask for.

They have a great example of C-js interop here: https://github.com/gasman/bonsai-c/tree/master/examples/mandelbrot

To use bonsai, it looks like you run the .c -> .asm.js transpiler on your local dev machine, then add the generated code to your git commit. The transpiler runs on node.js and can be installed with npm.