client/player.go

152 lines
3.6 KiB
Go
Raw Normal View History

2014-04-23 14:28:06 -07:00
package client
2014-04-09 21:35:54 -07:00
import (
"math"
"math/rand"
2014-04-23 14:54:30 -07:00
"bitbucket.org/hackerbots/server"
2014-04-09 21:35:54 -07:00
"bitbucket.org/hackerbots/vector"
)
2014-04-09 23:11:28 -07:00
// 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.
2014-04-09 21:35:54 -07:00
type Player interface {
2014-05-11 23:13:00 -07:00
Update(bs *server.Boardstate) map[string]server.Instruction
2014-04-09 21:35:54 -07:00
}
2014-04-09 23:11:28 -07:00
// SimplePlayer is our default player and stands as a starting point for your
// own Player implementations.
2014-04-09 21:35:54 -07:00
type SimplePlayer struct {
2014-04-23 14:54:30 -07:00
me server.Robot
2014-05-11 23:11:58 -07:00
width, height float64
2014-04-23 14:54:30 -07:00
knownObstacles map[string]server.Obstacle
nearestEnemy *server.OtherRobot
2014-04-23 14:28:06 -07:00
fireat *vector.Point2d
moveto *vector.Point2d
2014-05-11 23:11:58 -07:00
speed float64
maxSpeed float64
safeDistance float64
2014-04-09 21:35:54 -07:00
}
2014-04-09 23:11:28 -07:00
// NewSimplePlayer simply returns a populated, usable *SimplePlayer
2014-05-11 23:11:58 -07:00
func NewSimplePlayer(width, height float64) *SimplePlayer {
2014-04-09 21:35:54 -07:00
return &SimplePlayer{
2014-04-23 14:54:30 -07:00
knownObstacles: make(map[string]server.Obstacle),
2014-04-09 21:35:54 -07:00
width: width,
height: height,
maxSpeed: 100,
safeDistance: 40,
}
}
2014-05-11 23:13:00 -07:00
// Update is our implementation of recieving and processing a server.Boardstate
// from the server
func (p *SimplePlayer) Update(bs *server.Boardstate) map[string]server.Instruction {
instructions := make(map[string]server.Instruction)
for _, bot := range bs.MyRobots {
p.me = bot
p.speed = 1000
if p.me.Health <= 0 {
continue
}
p.recon(bs)
p.navigate()
probe_point := p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))
instructions[bot.Id] = server.Instruction{
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
Probe: &probe_point,
}
2014-04-09 21:35:54 -07:00
}
2014-05-11 23:13:00 -07:00
return instructions
2014-04-09 21:35:54 -07:00
}
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
}
}
2014-04-23 14:54:30 -07:00
func (p *SimplePlayer) recon(bs *server.Boardstate) {
2014-05-11 23:13:00 -07:00
// XXX: need to keep track of seen objects ..
2014-04-09 21:35:54 -07:00
// 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
2014-05-11 23:11:58 -07:00
closest := math.Inf(1)
2014-04-09 21:35:54 -07:00
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
}
}
2014-04-09 23:11:28 -07:00
// Instruction is our default implementation of preparing a map of information
// to be sent to server.
2014-04-23 14:54:30 -07:00
func (p *SimplePlayer) Instruction() map[string]server.Instruction {
return map[string]server.Instruction{
2014-04-09 21:35:54 -07:00
p.me.Id: {
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
},
}
}
2014-04-23 14:28:06 -07:00
func (p *SimplePlayer) randomDirection() *vector.Point2d {
pt := vector.Vector2d{
2014-05-11 23:11:58 -07:00
X: rand.Float64() * p.width,
Y: rand.Float64() * p.height,
2014-04-09 21:35:54 -07:00
}.ToPoint()
return &pt
}
2014-04-23 14:28:06 -07:00
func (p *SimplePlayer) probe(destination vector.Point2d) bool {
2014-04-09 21:35:54 -07:00
// XXX: make test for this
for _, v := range p.knownObstacles {
2014-04-23 14:28:06 -07:00
collided, _, _ := vector.RectIntersection(
2014-04-09 21:35:54 -07:00
v.Bounds,
p.me.Position,
destination.Sub(p.me.Position),
)
if collided {
return false
}
}
return true
}