Improving basic swarm bot

This commit is contained in:
Fraser Graham 2014-04-27 00:49:16 -06:00
parent a2436fe8d8
commit f8ecde3e81
2 changed files with 85 additions and 66 deletions

View File

@ -30,6 +30,7 @@ var forceJSON = flag.Bool("json", false, "force json encoding")
func main() { func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
var gameId string var gameId string
flag.Parse() flag.Parse()
@ -39,6 +40,7 @@ func main() {
gameId = flag.Arg(0) gameId = flag.Arg(0)
} }
client.Verbose = *verbose
c := &client.Client{ c := &client.Client{
Server: *serverHostname, Server: *serverHostname,
Port: *port, Port: *port,

149
player.go
View File

@ -9,6 +9,8 @@ import (
"bitbucket.org/hackerbots/vector" "bitbucket.org/hackerbots/vector"
) )
var Verbose bool = false
// Player is the interface that is implemented when specifying non-default // Player is the interface that is implemented when specifying non-default
// player behavior. // player behavior.
// //
@ -22,90 +24,120 @@ type Player interface {
// own Player implementations. // own Player implementations.
type SimplePlayer struct { type SimplePlayer struct {
me server.Robot me server.Robot
width, height float32 width, height float64
knownObstacles map[string]server.Obstacle knownObstacles map[string]server.Obstacle
nearestEnemy *server.OtherRobot nearestEnemy *server.OtherRobot
fireat *vector.Point2d fireat *vector.Point2d
moveto *vector.Point2d moveto *vector.Point2d
speed float32 speed float64
maxSpeed float32 maxSpeed float64
safeDistance float32 safeDistance float64
} }
// NewSimplePlayer simply returns a populated, usable *SimplePlayer // NewSimplePlayer simply returns a populated, usable *SimplePlayer
func NewSimplePlayer(width, height float32) *SimplePlayer { func NewSimplePlayer(width, height float64) *SimplePlayer {
return &SimplePlayer{ return &SimplePlayer{
knownObstacles: make(map[string]server.Obstacle), knownObstacles: make(map[string]server.Obstacle),
width: width, width: width,
height: height, height: height,
maxSpeed: 100, maxSpeed: 1000,
safeDistance: 40, safeDistance: 50,
} }
} }
// Recv is our implementation of receiving a server.Boardstate from the server // Recv is our implementation of receiving a server.Boardstate from the server
func (p *SimplePlayer) Update(bot *server.Robot, bs *server.Boardstate) server.Instruction{ func (p *SimplePlayer) Update(bot *server.Robot, bs *server.Boardstate) server.Instruction{
instruction := server.Instruction{ p.me = *bot
MoveTo: nil, p.speed = 1000
TargetSpeed: nil, if p.me.Health <= 0{
FireAt: nil, return server.Instruction{}
}
p.speed = p.maxSpeed
if len(bs.MyRobots) > 0 {
p.me = bs.MyRobots[0]
} else {
return instruction
} }
p.recon(bs) p.recon(bs)
p.navigate() p.navigate()
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{ return server.Instruction{
MoveTo: p.moveto, MoveTo: p.moveto,
TargetSpeed: &p.speed, TargetSpeed: &p.speed,
FireAt: p.fireat, FireAt: p.fireat,
Probe: &probe_point,
} }
} }
func (p *SimplePlayer) navigate() { func (p *SimplePlayer) navigate() {
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
}
if p.moveto == nil { if p.moveto == nil {
p.moveto = p.randomDirection() p.moveto = p.randomDirection()
// p.speed = p.maxSpeed
if Verbose {
fmt.Printf("Start\n")
}
return
} }
togo := p.me.Position.Sub(*p.moveto).Mag() togo := p.me.Position.Sub(*p.moveto).Mag()
if togo < p.safeDistance+5 { if togo < p.safeDistance+5 {
p.moveto = p.randomDirection() p.moveto = p.randomDirection()
return // p.speed = p.maxSpeed
if Verbose {
fmt.Printf("New Dest\n")
} }
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 return
} }
} }
func (p *SimplePlayer) recon(bs *server.Boardstate) { 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: // simplest shooting strategy ... need to do the following:
// not shoot through buildings // not shoot through buildings
// shoot at where the robot will be, not where it was. // shoot at where the robot will be, not where it was.
p.nearestEnemy = nil p.nearestEnemy = nil
p.fireat = nil p.fireat = nil
closest := float32(math.Inf(1)) closest := math.Inf(1)
for _, enemy := range bs.OtherRobots { for _, enemy := range bs.OtherRobots {
dist := p.me.Position.Sub(enemy.Position).Mag() dist := p.me.Position.Sub(enemy.Position).Mag()
if dist < closest && dist > p.safeDistance { if dist < closest && dist > p.safeDistance {
@ -118,10 +150,23 @@ func (p *SimplePlayer) recon(bs *server.Boardstate) {
} }
} }
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
}
}
}
func (p *SimplePlayer) randomDirection() *vector.Point2d { func (p *SimplePlayer) randomDirection() *vector.Point2d {
pt := vector.Vector2d{ pt := vector.Vector2d{
X: rand.Float32() * p.width, X: rand.Float64() * p.width,
Y: rand.Float32() * p.height, Y: rand.Float64() * p.height,
}.ToPoint() }.ToPoint()
return &pt return &pt
} }
@ -140,31 +185,3 @@ func (p *SimplePlayer) probe(destination vector.Point2d) bool {
} }
return true return true
} }
// MiniObstacle is a convenient way to encode/decode between the [4]int -> server.Obstacle
type MiniObstacle [4]int
// id is used to calculate a key for use in maps
func (mo *MiniObstacle) Id() string {
return fmt.Sprintf(
"%x%x%x%x",
mo[0],
mo[1],
mo[2],
mo[3],
)
}
func (mo MiniObstacle) String() string {
return mo.Id()
}
// ToObstacle is where the conversion magic happens
func (mo *MiniObstacle) ToObstacle() server.Obstacle {
return server.Obstacle{
Bounds: vector.AABB2d{
A: vector.Point2d{X: float32(mo[0]), Y: float32(mo[1])},
B: vector.Point2d{X: float32(mo[2]), Y: float32(mo[3])},
},
}
}