Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

WIP: Initial task oriented example. #275

Open
15 of 19 tasks
clemens-tolboom opened this issue Jun 1, 2016 · 56 comments
Open
15 of 19 tasks

WIP: Initial task oriented example. #275

clemens-tolboom opened this issue Jun 1, 2016 · 56 comments

Comments

@clemens-tolboom
Copy link
Collaborator

clemens-tolboom commented Jun 1, 2016

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)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • 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

@Defimatt
Copy link
Collaborator

Defimatt commented Jun 1, 2016

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?

@ermiyaeskandary
Copy link
Owner

ermiyaeskandary commented Jun 1, 2016

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

@Defimatt
Copy link
Collaborator

Defimatt commented Jun 1, 2016

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

@ermiyaeskandary
Copy link
Owner

ermiyaeskandary commented Jun 1, 2016

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

@Defimatt
Copy link
Collaborator

Defimatt commented Jun 1, 2016

@ermiyaeskandary correct.

@ermiyaeskandary
Copy link
Owner

ermiyaeskandary commented Jun 1, 2016

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

@Defimatt
Copy link
Collaborator

Defimatt commented Jun 1, 2016

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! 👍

@clemens-tolboom
Copy link
Collaborator Author

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
Copy link
Collaborator Author

  • 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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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

@ermiyaeskandary
Copy link
Owner

When do you want it to run ?

@clemens-tolboom
Copy link
Collaborator Author

When a new game starts.

@clemens-tolboom
Copy link
Collaborator Author

slither_io

@ermiyaeskandary
Copy link
Owner

ermiyaeskandary commented Jun 2, 2016

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

@ermiyaeskandary
Copy link
Owner

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

@clemens-tolboom
Copy link
Collaborator Author

Whenever a new play starts.

@ermiyaeskandary
Copy link
Owner

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
Copy link
Owner

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

@Seple
Copy link

Seple commented Jun 2, 2016

You can add to the menu the key ?

@clemens-tolboom
Copy link
Collaborator Author

Two bots directed to the same location (upper right)

slither_io_and_slither_io

@Seple
Copy link

Seple commented Jun 3, 2016

I'm keeping my fingers crossed for you team programer 👍
Thank you!

@clemens-tolboom
Copy link
Collaborator Author

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

@Seple
Copy link

Seple commented Jun 3, 2016

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

clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
clemens-tolboom added a commit that referenced this issue Jul 11, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants