fixed stalling robots

This commit is contained in:
Stephen McQuay 2014-01-11 23:14:44 -08:00
parent b16886cbac
commit a122577019

View File

@ -12,7 +12,8 @@ import (
) )
const maxSpeed = 100 const maxSpeed = 100
const safeDistance = 20 const safeDistance = 40
const maxSearchIterations = 20
func connect(server string, port int) (*websocket.Conn, error) { func connect(server string, port int) (*websocket.Conn, error) {
origin := "http://localhost/" origin := "http://localhost/"
@ -136,7 +137,6 @@ func (r *robot) play() {
if len(r.boardstate.MyRobots) > 0 { if len(r.boardstate.MyRobots) > 0 {
r.me = r.boardstate.MyRobots[0] r.me = r.boardstate.MyRobots[0]
} else { } else {
// XXX
log.Println("continue") log.Println("continue")
continue continue
} }
@ -178,7 +178,7 @@ func (r *robot) recon() {
} }
} }
if r.nearestEnemy != nil { if r.nearestEnemy != nil {
point := r.nearestEnemy.Position.Add(r.nearestEnemy.Heading.Scale(20)) point := r.nearestEnemy.Position.Add(r.nearestEnemy.Heading.Scale(safeDistance))
r.fireat = &point r.fireat = &point
} }
} }
@ -193,7 +193,7 @@ func (r *robot) selectDirection() *govector.Point2d {
func (r *robot) probe(destination govector.Point2d) bool { func (r *robot) probe(destination govector.Point2d) bool {
// XXX: make test for this // XXX: make test for this
for i := 0; i < 20; i++ { for i := 0; i < maxSearchIterations; i++ {
for _, v := range r.knownObstacles { for _, v := range r.knownObstacles {
collided, _, _ := govector.RectIntersection( collided, _, _ := govector.RectIntersection(
v.Bounds, v.Bounds,
@ -225,10 +225,10 @@ func (r *robot) navigate() {
return return
} }
togo := r.me.Position.Sub(*r.moveto).Mag() togo := r.me.Position.Sub(*r.moveto).Mag()
if togo < 20.0 { if togo < safeDistance {
r.moveto = r.selectDirection() r.moveto = r.selectDirection()
} }
if !r.probe(r.me.Position.Add(r.me.Heading.Scale(80))) { if !r.probe(r.me.Position.Add(r.me.Heading.Scale(safeDistance))) {
r.speed = 0 r.speed = 0
if !r.probe(*r.moveto) { if !r.probe(*r.moveto) {
r.moveto = r.selectDirection() r.moveto = r.selectDirection()