Slither.io-bot: WIP: Initial task oriented example.

Created on 1 Jun 2016  路  56Comments  路  Source: ErmiyaEskandary/Slither.io-bot

Meta

Description

As discussed in #266 but not completed yet here is my first stab.

My basic implementation uses a tasks list with priorities. I have added a few task

  • CheckForFood: Trigger food scan. It has a changing priority to allow other tasks to be performed.
  • MoveToXY move to the given waypoint
  • AvoidCollisionEnemySnake: Avoid collision with other (enemy) snakes. It using existing code.
  • ListTasks: List the current set of tasks. Has lowest priority

Motivation and Context

Problem

Adding new behaviour is difficult as this is hard coded. With A* it was still coded but easier to add new behavior. Furthermore we have a check for food triggered by a window timer which makes it harder too. It is not possible to add behaviour by another user script either.

Solution

This branch decouples the code

// Main bot
go: function() {

by replacing the parts mentioned above into a task scheduler. This allows for prioritise / (de)activate / adding tasks so the behaviour is more dynamic and alterable from outside this project (ie console).

It adds menu entries to toggle tasks to test particular behaviour.

Features

// Activate task
window.scheduler.getTask('MoveToXY').active = true;

// Set new point
window.scheduler.getTask('MoveToXY').point = {x: 21600, y: 21600};

// Prepare a new Task
var newTask = window.scheduler.newTask('dummy')

// Add new task
window.scheduler.addTask(newTask);

// Cannot add duplicate task ID
window.scheduler.addTask(window.scheduler.newTask('dummy'));

// Delete task
window.scheduler.deleteTask('dummy');

// Report on tasks
window.scheduler.getTask('ListTasks').execute();

Task list

window.scheduler.listTasks()

CheckForFood Active: true Priority: 397 Trigger food scan
AvoidCollisionEnemySnake Active: true Priority: 0 Avoid collision with other (enemy) snakes
_default Active: true Priority: 0 This is the default task which cannot be deactivated.
MoveToXY Active: false Priority: 300 Move to the given way point

Screenshots (if appropriate):

Menu

slither_io

MoveToXY

slither_io

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x] My code adheres to the code style of this project but most importantly of all to the things mentioned in CONTRIBUTING.md and PULL_REQUEST_TEMPLATE.md.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.

TODOs

  • [x] Fix priority for MoveToXY (stop when reached; lower priority when nearing P(x,y)?
  • [x] MoveToXY is now hard coded as move to center and never looses priority. It makes the bot reaching center.
  • [x] Add surround algorithm from A* branch
  • [x] Add more from A* branch
  • [x] Receive lots of feedback
  • [x] Provide task getter function getTaskByID
  • [x] Add default task
  • [x] Remove ListTasks as that is no actual task
  • [x] Add task.active boolean
  • [x] Check task structure so far.
  • [x] Add schedule.init() when entering game
  • [x] #285 Create issue when PR lands regarding __ AvoidCollisionEnemySnake __ as mentioned in https://github.com/ErmiyaEskandary/Slither.io-bot/issues/275#issuecomment-223595741
  • [x] #284 Create issue when PR lands to split collision snake and wall
in-progress feature

Most helpful comment

Excited to test this!

All 56 comments

Ok so I've been trying to test this branch. As per the example in the code, I ran...

bot.gotoXY = {x:10, y:10, active: true, priority: 300};
Object {x: 10, y: 10, active: true, priority: 300}

I then got the following error:

VM118:107 Uncaught TypeError: Cannot read property 'x' of undefined
window.canvasUtil.mapToMouse @ VM118:107
window.bot.tasks.execute @ VM118:936
window.bot.executeTasks @ VM118:1001
window.bot.go @ VM118:1012
window.userInterface.oefTimer @ VM118:1429

The whole game froze, and not only that I could only close the tab by killing chrome.exe. @clemens-tolboom what's going on?

@MattDuffin What's the specific line of code? Don't currently have access to a computer :(

@ErmiyaEskandary I think I've figured it out. The code example in bot.tasks --> MoveToXY is bot.gotoXY = {x:X, y:Y, active: true, priority: 300}; but when I read through the code, it should be something like bot.gotoXY = {point: {x:X, y:Y}, active: true, priority: 300}; . That resulted in the mapToMouse function barfing.

@clemens-tolboom Can you please keep the code comments / examples updated as you change the code? It's annoying to have to kill the whole browser.

@MattDuffin Great - so the example should be :
bot.gotoXY = {point: {x:20000, y:20000}, active: true, priority: 300};
(20000,20000) is the middle

@ErmiyaEskandary correct.

@clemens-tolboom @MattDuffin Currently going towards the middle but doesn't check for food alot but a great start!

Ok, so having tested this out, I like the idea. It's fun to watch the bot act like it's got a sense of purpose in going to a specific position, and if I want to chill out by one of the walls for a while so I'm not bothered by other snakes, I can set {x: 100, y: 100} or whatever instead of going to the middle.

It's good that it looks out for food a bit when it can (and you see the target coordinates change as it's doing so) then goes back to its target, but it misses out on a lot of dead snake food that's right in front of it. I guess you can override that by changing the priority.

However, it does tend to crash into snakes a lot more when going to a coordinate, and I think that collision avoidance should be its first priority, even when navigating.

The code is a good start though! :+1:

Thanks for testing that fast. Awesome.

This issue has some TODOs of which the moveToXY definitely needs a fix

I add the following to the issue summary

  • Provide task getter function getTaskByID
  • Add default task
  • Add task.active boolean
  • Check task structure so far.
    Then the general structure could be OK.

And removed

  • Add surround algorithm from A* branch
  • Add more from A* branch
    as these otherwise could block the PR to be.
  • Task MoveToXY now gets inactive when reached the point. (That open ups new Task Waypoints to feed MoveToXY)
  • MoveToXY is refactored deleting bot.GotoXY as that was a silly idea and another global var.
  • While working on the CRUD for tasks it probably would be better to move all task related stuff out of the bot and into its own window.task variable.
  • One big issue is task are cross game statefull: When ie MoveToXY is deactivated it will not be activated for the next game. Which implies I have to do one more refactoring.
  • Some example code (still in the branch as it activates move to center by default. Should we do that.
// Activate task
bot.getTask('MoveToXY').active = true;

// Set new point
bot.getTask('MoveToXY').point = {x: 21600, y: 21600};

// Add new task
bot.addTask(bot.newTask('dummy'));

// Cannot add duplicate task ID
bot.addTask(bot.newTask('dummy'));

// Delete task
bot.deleteTask('dummy');

// Deleted correct item?
console.log('No dummy found!', bot.getTask('dummy'));

// Report on tasks
bot.getTask('ListTasks').execute();

I'm satisfied now

scheduler.init();

// Activate task
scheduler.getTask('MoveToXY').active = true;

// Set new point
scheduler.getTask('MoveToXY').point = {x: 21600, y: 21600};

// Add new task
scheduler.addTask(scheduler.newTask('dummy'));

// Cannot add duplicate task ID
scheduler.addTask(scheduler.newTask('dummy'));

// Delete task
scheduler.deleteTask('dummy');

// Deleted correct item?
console.log('No dummy found!', scheduler.getTask('dummy'));

// Report on tasks
scheduler.getTask('ListTasks').execute();

The schedule.init() must be called somewhere when entering the game. Dunno where :-( Please fix

When do you want it to run ?

When a new game starts.

slither_io

Does it need to run continuously ?
Doh - schedule.init() - it only runs once

If it only needs to be ran once, stick it somewhere here - https://github.com/ErmiyaEskandary/Slither.io-bot/blob/master/bot.user.js#L1359

Whenever a new play starts.

Perhaps in https://github.com/ErmiyaEskandary/Slither.io-bot/blob/master/bot.user.js#L1306 as the scores also get updated whenever a new play starts ?

I've also invited you to Gitter at clem**@build2be.com - most of the dev talk goes on here!

You can add to the menu the key ?

Two bots directed to the same location (upper right)

slither_io_and_slither_io

I'm keeping my fingers crossed for you team programer :+1:
Thank you!

@Seple adding one key is not an option as we want to toggle all available tasks. I'll try to use number keys to attach the task. Then we have a start.

So we misunderstand. I though you meant enable/disable tasks. This issue is about open up the code flow. MoveToXY was an example. We need a scheduler to allow others to set priorities or add new behaviours.

I'm testing now for below. ((de)active a task)
slither_io

See summary for your MoveToXY widget. I prefer the console for now and my C&C user script to be.

@Seple I've added the toggle tasks into menu.

Note: Disabling the AvoidCollisionEnemySnake task does not stops avoiding snakes. That has nothing to do with this patch as I reuse that code. This issue only shows that code part(s) need some refactoring.

We should make this a PR ASAP. Not sure what could block this now.

@ErmiyaEskandary shall we PR this now?

@clemens-tolboom So at this stage, what does your branch have? What does it do? What can it be used for? What issues does it solve or help in solving if it does?

@ErmiyaEskandary I've updated the issue summary. Hope that's clear enough :-)

Should I fix this _Menu and score overlap_ in this issue?

slither_io

Added TODOs

@clemens-tolboom Yes - please fix the overlap. So this basically cleans up the code and allows for more behaviour controls? Where would this be actually applicable?
Also, please add a default task as that would cause an issue with autorespawning and then doing nothing.

So this basically cleans up the code and allows for more behaviour controls?

Yes.

Where would this be actually applicable?

  • We can control testing better. IE by issuing a MoveToXY(center) or avoid a center.
  • Using slither-nest we could ie MoveToXY(nearWall) 4 snakes to battle each other for test purposes
  • I'm building a Command&Control node.js combined with slither-nest and working on commands 'FollowTheLeader', etc.
  • Sky is the limit ;-)
  • Fixed: overlap
  • Fixed: Added _default and do not show in menu
  • I've removed the example task from code
{
    id: 'AvoidCollisionWall',
    // A use case is to feed ones friend
    description: 'There is no use in dying against the wall'
    getPriority: function() {
        return 1000;
    }
},
{
    id: 'KillEnemy',
    description: 'This is a suicide action useful when having a friend(s).',
    getPriority: function() {
        return 0;
    }
},
{
    id: 'AvoidCollisionFriend',
    // do not collide with friends
    getPriority: function() {
      return 1000;
    }
},

Ah ... Seems like a great thing then... :+1: gj
Are you done with the changes ? Is it possible for you to port slither-nest as well ?

Excited to test this!

Created followup issues

In order to make the PR I have to do a

git checkout feature/task-execution
git pull --rebase develop

that way the merge will be smooth.

Is anyone working on this branch?

@clemens-tolboom I don't think anybody is - you came up with it :+1:

Once you've rebased and opened a PR against develop I will run a head-to-head test overnight.

Rebased on latest develop.

I somehow thought sometime was wrong with the bot.

While rebasing I saw my mistake. I removed

if (bot.checkCollision()) {
-                bot.lookForFood = false;
-                if (bot.foodTimeout) {
-                    window.clearTimeout(bot.foodTimeout);
-                    bot.foodTimeout = window.setTimeout(
-                        bot.foodTimer, 1000 / bot.opt.targetFps * bot.opt.foodFrames);
-                }
-            } else {
-                bot.lookForFood = true;
-                if (bot.foodTimeout === undefined) {
-                    bot.foodTimeout = window.setTimeout(
-                        bot.foodTimer, 1000 / bot.opt.targetFps * bot.opt.foodFrames);

and replaced it by a sawtooth priority to allow other tasks to run too.

                   // We don't want to block other task so let priority change from
                    startPriority: 200,
                    // to maximum priority
                    triggerPriority: 400,

                    getPriority: function () {
                        var currentPriority = this.priority;

                        bot.computeFoodGoal(); // MOVE DOWN
                        if (bot.currentFood) {
                            window.setAcceleration(bot.foodAccel());
                            if (this.priority < this.triggerPriority) {
                                // Increment priority to trigger bot.computeFoodGoal
                                return this.priority + 1; // ======== CHANGE HERE
                            }
                        }
                        return this.startPriority;
                    },

When no other task are available it should run every 200 frames (priority from 200 - 400)

1000 / bot.opt.targetFps * bot.opt.foodFrames

I think is should be

                    getPriority: function () {
                        var currentPriority = this.priority;

                        if (bot.currentFood) {
                            window.setAcceleration(bot.foodAccel());
                        }
                        if (this.priority < this.triggerPriority) {
                            // Increment priority to trigger bot.computeFoodGoal
                            return this.priority + formula wiht (this.triggerPriority - this.startPriority) and 1000 / bot.opt.targetFps * bot.opt.foodFrames
                        }
                        else {
                            bot.computeFoodGoal();
                        }
                        return this.startPriority;
                    },

Bottom line: help :(

Below some results without MoveToXY which are not good I guess :-(

I have not much knowledge about the bots logic:

  • Avoid collision
  • Find a direction for food cluster
  • Move to food

It seems to go fast less often :-(
Enabling MoveToXY is now messy interfering with Move to food :-(

(I did Chrome inspect element then copy/paste)

games played: 92
a: 201 m: 39
1. 5001
2. 3113
3. 1405
4. 1272
5. 1070
6. 949
7. 708
8. 296
9. 296
10. 254

games played: 72
a: 450 m: 35
1. 8198
2. 7661
3. 7320
4. 5228
5. 225
6. 210
7. 210
8. 207
9. 205
10. 205
games played: 374
a: 291 m: 44
1. 20631
2. 11552
3. 10789
4. 9763
5. 7796
6. 6627
7. 4328
8. 4328
9. 1816
10. 1280

@clemens-tolboom PHEW - THAT'S ALOT OF GAMES....
But the average doesn't yield good feelings...

I've created a PR as I think I nailed it. Not 100% sure but other eyes fix issues

@clemens-tolboom Needs testing but great job! :+1:
@MattDuffin @ChadSki @j-c-m @tjorim @Drflash55 #292 please

@clemens-tolboom

Below some results without MoveToXY which are not good I guess :-(

Solved ?

The only useful feature ----> move toXY
Please upgrade
The exact locations on the map
Entered with the finger - look ---> http://s33.postimg.org/ilte1amxr/image.jpg
or (option click mouse) select on the radar

I'm afraid we have a complete misunderstand. Let's please schedule a Google Hangout to discuss why I spend my time for this.

MoveToXY is just an example!!!

Problem
Adding new behaviour is difficult as this is hard coded. With A* it was still coded but easier to add new behaviour. Furthermore we have a check for food triggered by a window timer which makes it harder too. It is not possible to add behaviour by another user script either.

Audio chat is a good idea.

@ErmiyaEskandary and @clemens-tolboom did a Google Hangout but that was quite bad quality. Not sure what happened.

The talk was great to have and Screen sharing a must.

We agreed on a mismatch in my code regarding Eat, CheckForFood and MoveToXY on which I have to chew first.

quite bad quality. Not sure what happened.

Blame Google lol - the rest was fine lol :+1:

I suggest changing the default priority of MoveToXY to 40. Snake seems less pathological about traveling taking more time to eat.

Can MoveToXY be changed to MoveToRadius?

No matter where the snake is move to certain distance from center. Imagine four quadrants. If snake is in upper left quadrant and the XY is off center in bottom right quadrant is suboptimal. I am not fond of using dead center XY

@khorth I'm fine with setting the default lower but not sure what for others

@phrokton
I use MoveToXY to direct two snakes to a point. Let one snake follow the other.
But we can add a MoveToRadius which is in a way a move to a food density

@khorth hmmm I was a little too hasty. The puzzle to solve with priorities is highest priority wins.

  • AvoidCollisionEnemySnake 500 (collision risk detected) or 0
  • SnakeParamsBySize (has no execute as its getpriority adjust params)
  • HuntForPrey (450 or 0) checked every bot.opt.foodFrames frame
  • CheckForFood (400 or 0) checked every bot.opt.foodFrames frame
  • Eat (350) checked every frame
  • MoveToXY (300 or 0) checked every frame

One has to disable Eat to even get a MoveToXY. But then CheckForFood triggers now and then making the bot moving like a drunk.

It makes no sense lowering the priority. It is about the orchestrating of the tasks. I'm not sure I did that right :-(

I'm not sure how to fix this.

Please add a new function

"Escape mode"
-escape the blank maps

"mode 2"
-in the direction of clusters of food

"mode 3"
-the center of the map

missing too... "Defense mode" #78 and "Hunting mode"

MoveToXY is really a move to (x,y) so we should not change that. Adding
more tasks like MoveToCenter (which is a MoveToXY(21000,21000) iirc) and
MoveToRadius(1000) would make sense if a user activate those.

Escape blanks maps
That would be the FindFood logic I guess

Do we have defense / hunting modes :-)

Adding new tasks is a little futile as there is no intent to merge this
issue afaik. It would be a moving target.

I still like the idea but we need to fix the behavior problem (bot
should do some task in a sequence ie MoveToCenter + Eat) to make more
sense similar how Slither IO does it's ugly AI (move around then sprint etc)

OK I understand :)

"Escape mode"
-escape the blank maps
Maybe priority mode of speed escape?
http://s33.postimg.org/egkz7unkf/New_option.jpg

by the way please also...
Please correct mode "HuntForPrey" + add / merge function "acceleration speed - Fast Boost"
Possible improvement ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EatSnakes picture EatSnakes  路  3Comments

takeachillphill picture takeachillphill  路  8Comments

Salvationdk picture Salvationdk  路  5Comments

Tomboman picture Tomboman  路  5Comments

Fredyy90 picture Fredyy90  路  3Comments