Skip to content

Commit

Permalink
Improvements on attacker
Browse files Browse the repository at this point in the history
  • Loading branch information
GaMendes committed Mar 8, 2019
1 parent 0074bb2 commit a4a53ca
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 16 deletions.
34 changes: 21 additions & 13 deletions lib/Coach.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ const GoalKeeper = require('../players/experimental/GoalkeeperMain')
const GoalKeeperSides = require('../players/experimental/GoalkeeperSides')
const GoalKeeperPush = require('../players/experimental/GoalkeeperPush')
const FieldConstraints = require('../entities/FieldConstraints')

const Attacker = require('../players/experimental/Attacker')
const AttackerFoward = require('../players/experimental/AttackerFoward')

const Rules = require('../players/experimental/RulePlays')

const Vector = require('../lib/Vector')
const AttackerSides = require('../players/experimental/AttackerSides')
const AttackerAvoidSides = require('../players/experimental/AttackerAvoidSides')

module.exports = class Coach {
constructor(dependencies) {
this.match = dependencies.match
this.robotsProperties = this.match.robotsProperties

this.currentOrder = {}

this.init()
}

Expand All @@ -38,7 +34,9 @@ module.exports = class Coach {

this.attacker = {
main: new Attacker(0, this.match, null),
foward: new AttackerFoward(0, this.match, null)
foward: new AttackerFoward(0, this.match, null),
sides: new AttackerSides(0, this.match, null),
avoidSides: new AttackerAvoidSides(0, this.match, null)
}

}
Expand Down Expand Up @@ -97,12 +95,8 @@ module.exports = class Coach {
})

_.values(robots).map(robot => {
let robotPos = robot.robots.self.position
let ballPos = this.match.dataManager.ball
if(robot.radioId == this.goalkeeperId) {

if (!ball) robot.runningPlay = this.goalkeeper.main

if (this.insideFieldConstraint(FieldConstraints.SMALL_AREA_CENTER, ball)) {
robot.runningPlay = this.goalkeeper.push
this.goalkeeper.push.setRobot(robot)
Expand All @@ -121,12 +115,26 @@ module.exports = class Coach {
let ballToCenterGoal = Vector.sub({x: 700, y: 0}, ball)
let robotToBall = Vector.sub(ball, robotPos)
let angle = Vector.toDegrees(Vector.angleBetween(ballToCenterGoal, robotToBall))
if(Math.abs(angle) < 10) {
if (Math.abs(ball.y) > 500 && Math.abs(robotPos.y) > 500) {

if (robotPos.x < ball.x) {
robot.runningPlay = this.attacker.sides
this.attacker.sides.setRobot(robot)
console.log('laterais')
} else {
robot.runningPlay = this.attacker.avoidSides
this.attacker.avoidSides.setRobot(robot)
console.log('laterais atras')
}

} else if(Math.abs(angle) < 10) {
robot.runningPlay = this.attacker.foward
this.attacker.foward.setRobot(robot)
}else {
console.log('atacando')
} else {
robot.runningPlay = this.attacker.main
this.attacker.main.setRobot(robot)
console.log('padrão')
}

} else {
Expand Down
35 changes: 35 additions & 0 deletions players/experimental/AttackerAvoidSides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const IntentionPlayer = require('../IntentionPlayer')
const TensorMath = require('../../lib/TensorMath')
const Intention = require('../../Intention')
const LineIntention = require('../../Intention/LineIntention')
const PointIntention = require('../../Intention/PointIntention')
const LookAtIntention = require('../../Intention/LookAtIntention')
const OrbitalIntention = require('../../Intention/OrbitalIntention')
const Vector = require('../../lib/Vector')
const RulePlays = require('./RulePlays')

const BASE_SPEED = 100

module.exports = class AttackerAvoidSides extends RulePlays {
setup () {
super.setup()
let ball = () => {
let ball = {x: this.match.dataManager.ball.x, y: this.match.dataManager.ball.y}
let ballSide = ball.y/Math.abs(ball.y)


return {x: ball.x, y: ball.y - ballSide * 100}
}

this.addIntetion(new PointIntention('goBallSides', {
target: ball,
radius: 150,
decay: TensorMath.new.constant(1).deadWidth(0.15).finish,
multiplier: 60
}))

}
loop(){

}
}
30 changes: 27 additions & 3 deletions players/experimental/AttackerFoward.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ const RulePlays = require('./RulePlays')
const BASE_SPEED = 65

module.exports = class AttackerForward extends RulePlays {

isLeft(P0, P1, P2 ) {
return ( (P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y) )
}
pointInRectangle(X, Y, Z, W, P) {
return (this.isLeft(X, Y, P) > 0 && this.isLeft(Y, Z, P) > 0 && this.isLeft(Z, W, P) > 0 && this.isLeft(W, X, P) > 0)
}

setup () {
this.attackingLock = false

super.setup()
let ball = () => {
let ball = {x: this.match.dataManager.ball.x, y: this.match.dataManager.ball.y}
Expand Down Expand Up @@ -52,16 +62,30 @@ module.exports = class AttackerForward extends RulePlays {
}))

}
loop(){
loop () {
let ball = {x: this.match.dataManager.ball.x, y: this.match.dataManager.ball.y}
let robotDir = Vector.fromTheta(this.orientation)
let robotDir = Vector.norm(Vector.fromTheta(this.orientation))
let robotToBall = Vector.sub(this.position, ball)
let angle = Vector.toDegrees(Vector.angleBetween(robotDir, robotToBall))
let normVector = Vector.fromTheta(Vector.toDegrees(this.orientation) + 90)
normVector = Vector.norm(normVector)
let robotLeft = {x: this.position.x + normVector.x * 40, y: this.position.y + normVector.y * 40}
let robotRight = {x: this.position.x - normVector.x * 40, y: this.position.y - normVector.y * 40}
let robotLeftFinal = {x: robotLeft.x + robotDir.x * 400, y: robotLeft.y + robotDir.y * 400}
let robotRightFinal = {x: robotRight.x + robotDir.x * 400, y: robotRight.y + robotDir.y * 400}

if (Math.abs(angle) > 20 && Math.abs(angle) < 160) {
let insideRectangle = this.pointInRectangle(robotLeft, robotRight, robotLeftFinal, robotRightFinal, ball)
if (this.attackingLock && insideRectangle) {
this.attackingLock = true
this.$lookAtBall.weight = 0
this.$Attack.weight = 1
this.$followBall = 0.5
}
else if (Math.abs(angle) > 20 && Math.abs(angle) < 160 && !this.attackingLock) {
this.$lookAtBall.weight = 1
this.$Attack.weight = 0
} else {
this.attackingLock = true
this.$lookAtBall.weight = 0
this.$Attack.weight = 1
this.$followBall = 0.5
Expand Down
55 changes: 55 additions & 0 deletions players/experimental/AttackerSides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const IntentionPlayer = require('../IntentionPlayer')
const TensorMath = require('../../lib/TensorMath')
const Intention = require('../../Intention')
const LineIntention = require('../../Intention/LineIntention')
const PointIntention = require('../../Intention/PointIntention')
const LookAtIntention = require('../../Intention/LookAtIntention')
const OrbitalIntention = require('../../Intention/OrbitalIntention')
const Vector = require('../../lib/Vector')
const RulePlays = require('./RulePlays')

const BASE_SPEED = 65

module.exports = class AttackerSides extends RulePlays {

setup () {
super.setup()

let ball = () => {
let ball = {x: this.match.dataManager.ball.x, y: this.match.dataManager.ball.y}
return ball
}

this.addIntetion(new PointIntention('goBallSides', {
target: ball,
radius: 150,
decay: TensorMath.new.constant(1).deadWidth(0.15).finish,
multiplier: 60
}))

this.addIntetion(new LineIntention('avoidHitSide', {
target: {x: 0, y: -655},
theta: Vector.direction("right"),
lineSize: 2000, // Largura do segmento de reta
lineDist: 100, // Tamanho da repelência
lineDistMax:100,
lineDistSingleSide: true,
decay: TensorMath.new.mult(-1).finish,
multiplier: 30
}))

this.addIntetion(new LineIntention('avoidHitSide', {
target: {x: 0, y: 655},
theta: Vector.direction("left"),
lineSize: 2000, // Largura do segmento de reta
lineDist: 100, // Tamanho da repelência
lineDistMax:100,
lineDistSingleSide: true,
decay: TensorMath.new.mult(-1).finish,
multiplier: 30
}))
}

loop () {}

}

0 comments on commit a4a53ca

Please sign in to comment.