client/player.go

143 lines
3.4 KiB
Go

package client
import (
"math"
"math/rand"
"bitbucket.org/hackerbots/server"
"bitbucket.org/hackerbots/vector"
)
// Player is the interface that is implemented when specifying non-default
// player behavior.
//
// The general case will be to implement a Player type that contains the magic
// required to slay other robots quickly while staying alive for a long time.
type Player interface {
Recv(bs *server.Boardstate)
Instruction() map[string]server.Instruction
}
// SimplePlayer is our default player and stands as a starting point for your
// own Player implementations.
type SimplePlayer struct {
me server.Robot
width, height float64
knownObstacles map[string]server.Obstacle
nearestEnemy *server.OtherRobot
fireat *vector.Point2d
moveto *vector.Point2d
speed float64
maxSpeed float64
safeDistance float64
}
// NewSimplePlayer simply returns a populated, usable *SimplePlayer
func NewSimplePlayer(width, height float64) *SimplePlayer {
return &SimplePlayer{
knownObstacles: make(map[string]server.Obstacle),
width: width,
height: height,
maxSpeed: 100,
safeDistance: 40,
}
}
// Recv is our implementation of receiving a server.Boardstate from the server
func (p *SimplePlayer) Recv(bs *server.Boardstate) {
p.speed = p.maxSpeed
if len(bs.MyRobots) > 0 {
p.me = bs.MyRobots[0]
} else {
return
}
p.recon(bs)
p.navigate()
}
func (p *SimplePlayer) navigate() {
if p.moveto == nil {
p.moveto = p.randomDirection()
}
togo := p.me.Position.Sub(*p.moveto).Mag()
if togo < p.safeDistance+5 {
p.moveto = p.randomDirection()
return
}
if !p.probe(p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))) {
p.speed = 0
if !p.probe(*p.moveto) {
p.moveto = p.randomDirection()
return
}
}
if p.me.Collision != nil {
p.moveto = p.randomDirection()
p.speed = 0
return
}
}
func (p *SimplePlayer) recon(bs *server.Boardstate) {
for _, o := range bs.Objects {
obj := MiniObstacle(o)
if _, ok := p.knownObstacles[obj.Id()]; !ok {
p.knownObstacles[obj.Id()] = obj.ToObstacle()
}
}
// simplest shooting strategy ... need to do the following:
// not shoot through buildings
// shoot at where the robot will be, not where it was.
p.nearestEnemy = nil
p.fireat = nil
closest := math.Inf(1)
for _, enemy := range bs.OtherRobots {
dist := p.me.Position.Sub(enemy.Position).Mag()
if dist < closest && dist > p.safeDistance {
p.nearestEnemy = &enemy
}
}
if p.nearestEnemy != nil {
point := p.nearestEnemy.Position.Add(p.nearestEnemy.Heading.Scale(p.safeDistance))
p.fireat = &point
}
}
// Instruction is our default implementation of preparing a map of information
// to be sent to server.
func (p *SimplePlayer) Instruction() map[string]server.Instruction {
return map[string]server.Instruction{
p.me.Id: {
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
},
}
}
func (p *SimplePlayer) randomDirection() *vector.Point2d {
pt := vector.Vector2d{
X: rand.Float64() * p.width,
Y: rand.Float64() * p.height,
}.ToPoint()
return &pt
}
func (p *SimplePlayer) probe(destination vector.Point2d) bool {
// XXX: make test for this
for _, v := range p.knownObstacles {
collided, _, _ := vector.RectIntersection(
v.Bounds,
p.me.Position,
destination.Sub(p.me.Position),
)
if collided {
return false
}
}
return true
}