client/player.go

188 lines
4.3 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 (
"fmt"
"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-26 23:49:16 -07:00
var Verbose bool = false
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 {
Update(bot *server.Robot, bs *server.Boardstate) 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-04-26 23:49:16 -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-04-26 23:49:16 -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-04-26 23:49:16 -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,
2014-04-26 23:49:16 -07:00
maxSpeed: 1000,
safeDistance: 50,
2014-04-09 21:35:54 -07:00
}
}
2014-04-23 14:54:30 -07:00
// Recv is our implementation of receiving a server.Boardstate from the server
func (p *SimplePlayer) Update(bot *server.Robot, bs *server.Boardstate) server.Instruction{
2014-04-26 23:49:16 -07:00
p.me = *bot
p.speed = 1000
if p.me.Health <= 0{
return server.Instruction{}
2014-04-09 21:35:54 -07:00
}
p.recon(bs)
p.navigate()
2014-04-26 23:49:16 -07:00
probe_point := p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))
if Verbose {
fmt.Printf("PROBE SENT: %v\n",probe_point)
}
return server.Instruction{
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
2014-04-26 23:49:16 -07:00
Probe: &probe_point,
}
2014-04-09 21:35:54 -07:00
}
func (p *SimplePlayer) navigate() {
2014-04-26 23:49:16 -07:00
if Verbose {
fmt.Printf("%v S:%v H:%v TS:%v\n\tX:%v Y:%v\n\tHX:%v HY:%v\n",
p.me.Name, p.me.Speed, p.me.Health, p.me.TargetSpeed,
p.me.Position.X, p.me.Position.Y,
p.me.Heading.X, p.me.Heading.Y)
if p.me.MoveTo != nil {
fmt.Printf("\tTX:%v TY:%v\n",
p.me.MoveTo.X, p.me.MoveTo.Y)
}
}
// if !p.probe(p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))) {
// if !p.probe(*p.moveto) {
// p.moveto = p.randomDirectionDrift(p.moveto, 20)
// // p.speed = p.maxSpeed
// fmt.Printf("Obstacle?\n")
// return
// }
// }
if p.me.ProbeResult != nil {
p.moveto = p.randomDirectionDrift(&p.me.Position, 100)
p.speed = -20
if Verbose {
fmt.Printf("Probe %v\n", p.me.ProbeResult)
}
return
}
if p.me.Collision != nil {
p.moveto = p.randomDirectionDrift(&p.me.Position, 100)
p.speed = -20
if Verbose {
fmt.Printf("Hit!\n")
}
return
}
2014-04-09 21:35:54 -07:00
if p.moveto == nil {
p.moveto = p.randomDirection()
2014-04-26 23:49:16 -07:00
// p.speed = p.maxSpeed
if Verbose {
fmt.Printf("Start\n")
}
return
2014-04-09 21:35:54 -07:00
}
togo := p.me.Position.Sub(*p.moveto).Mag()
if togo < p.safeDistance+5 {
p.moveto = p.randomDirection()
2014-04-26 23:49:16 -07:00
// p.speed = p.maxSpeed
if Verbose {
fmt.Printf("New Dest\n")
2014-04-09 21:35:54 -07:00
}
return
}
}
2014-04-23 14:54:30 -07:00
func (p *SimplePlayer) recon(bs *server.Boardstate) {
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-04-26 23:49:16 -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-26 23:49:16 -07:00
func (p *SimplePlayer) randomDirectionDrift(start *vector.Point2d, drift float64) *vector.Point2d {
for {
pt := vector.Vector2d{
X: start.X - drift + rand.Float64() * drift * 2,
Y: start.Y - drift + rand.Float64() * drift * 2,
}.ToPoint()
if pt.X > 0 && pt.X < p.width && pt.Y > 0 && pt.Y < p.height {
return &pt
}
}
}
2014-04-23 14:28:06 -07:00
func (p *SimplePlayer) randomDirection() *vector.Point2d {
pt := vector.Vector2d{
2014-04-26 23:49:16 -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
}