Slither.io-bot icon indicating copy to clipboard operation
Slither.io-bot copied to clipboard

WIP: Initial task oriented example.

Open clemens-tolboom opened this issue 8 years ago • 56 comments

Meta

  • branch https://github.com/ErmiyaEskandary/Slither.io-bot/tree/feature/task-execution
  • PR #292

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

clemens-tolboom avatar Jun 01 '16 13:06 clemens-tolboom

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?

Defimatt avatar Jun 01 '16 15:06 Defimatt

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

ermiyaeskandary avatar Jun 01 '16 15:06 ermiyaeskandary

@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.

Defimatt avatar Jun 01 '16 15:06 Defimatt

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

ermiyaeskandary avatar Jun 01 '16 15:06 ermiyaeskandary

@ErmiyaEskandary correct.

Defimatt avatar Jun 01 '16 15:06 Defimatt

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

ermiyaeskandary avatar Jun 01 '16 15:06 ermiyaeskandary

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:

Defimatt avatar Jun 01 '16 15:06 Defimatt

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.

clemens-tolboom avatar Jun 01 '16 16:06 clemens-tolboom

  • 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();

clemens-tolboom avatar Jun 02 '16 07:06 clemens-tolboom

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();

clemens-tolboom avatar Jun 02 '16 07:06 clemens-tolboom

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

clemens-tolboom avatar Jun 02 '16 08:06 clemens-tolboom

When do you want it to run ?

ermiyaeskandary avatar Jun 02 '16 09:06 ermiyaeskandary

When a new game starts.

clemens-tolboom avatar Jun 02 '16 09:06 clemens-tolboom

slither_io

clemens-tolboom avatar Jun 02 '16 09:06 clemens-tolboom

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

ermiyaeskandary avatar Jun 02 '16 09:06 ermiyaeskandary

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

ermiyaeskandary avatar Jun 02 '16 09:06 ermiyaeskandary

Whenever a new play starts.

clemens-tolboom avatar Jun 02 '16 09:06 clemens-tolboom

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 ?

ermiyaeskandary avatar Jun 02 '16 09:06 ermiyaeskandary

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

ermiyaeskandary avatar Jun 02 '16 10:06 ermiyaeskandary

You can add to the menu the key ?

Seple avatar Jun 02 '16 21:06 Seple

Two bots directed to the same location (upper right)

slither_io_and_slither_io

clemens-tolboom avatar Jun 03 '16 06:06 clemens-tolboom

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

Seple avatar Jun 03 '16 08:06 Seple

@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.

clemens-tolboom avatar Jun 03 '16 13:06 clemens-tolboom

key or window look ---> http://s33.postimg.org/ilte1amxr/image.jpg

Seple avatar Jun 03 '16 13:06 Seple

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.

clemens-tolboom avatar Jun 03 '16 14:06 clemens-tolboom

@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 avatar Jun 03 '16 14:06 clemens-tolboom

@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 avatar Jun 03 '16 15:06 ermiyaeskandary

@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

clemens-tolboom avatar Jun 03 '16 15:06 clemens-tolboom

Added TODOs

  • Remove ListTasks as that is no actual task
  • Create issue when PR lands regarding __ AvoidCollisionEnemySnake __ as mentioned in https://github.com/ErmiyaEskandary/Slither.io-bot/issues/275#issuecomment-223595741

clemens-tolboom avatar Jun 03 '16 15:06 clemens-tolboom

@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.

ermiyaeskandary avatar Jun 03 '16 15:06 ermiyaeskandary