consider destination slightly before collision

also: added verbose blocks for logging
This commit is contained in:
Stephen McQuay 2014-01-12 00:03:17 -08:00
parent a122577019
commit 5b8ec0b8ab

View File

@ -44,7 +44,9 @@ type robot struct {
} }
func (r *robot) negociate() (err error) { func (r *robot) negociate() (err error) {
if *verbose {
log.Printf("%s: trying to connect to game '%s'", r.name, r.game.name) log.Printf("%s: trying to connect to game '%s'", r.name, r.game.name)
}
r.ws, err = connect(r.server, r.port) r.ws, err = connect(r.server, r.port)
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("connection failure: %s", err)) return errors.New(fmt.Sprintf("connection failure: %s", err))
@ -67,7 +69,9 @@ func (r *robot) negociate() (err error) {
if err != nil || idreq.Type == "failure" { if err != nil || idreq.Type == "failure" {
return errors.New(fmt.Sprintf("failure: %+v", idreq)) return errors.New(fmt.Sprintf("failure: %+v", idreq))
} }
if *verbose {
log.Printf("%s: idreq: %+v", r.name, idreq) log.Printf("%s: idreq: %+v", r.name, idreq)
}
err = websocket.JSON.Send(r.ws, struct { err = websocket.JSON.Send(r.ws, struct {
Type string `json:"type"` Type string `json:"type"`
@ -85,7 +89,9 @@ func (r *robot) negociate() (err error) {
if r.game.Type != "gameparam" { if r.game.Type != "gameparam" {
return errors.New("didn't receive a good gameparam") return errors.New("didn't receive a good gameparam")
} }
if *verbose {
log.Printf("%s: game parameters: %+v", r.name, r.game) log.Printf("%s: game parameters: %+v", r.name, r.game)
}
conf := ClientConfig{ conf := ClientConfig{
ID: r.game.name, ID: r.game.name,
@ -106,7 +112,9 @@ func (r *robot) negociate() (err error) {
return errors.New("failed to validate correct stats request") return errors.New("failed to validate correct stats request")
} }
r.playerId = handshake.Id r.playerId = handshake.Id
if *verbose {
log.Printf("%s: handshake: %+v", r.name, handshake) log.Printf("%s: handshake: %+v", r.name, handshake)
}
return nil return nil
} }
@ -128,20 +136,25 @@ func (r *robot) play() {
log.Printf("%s: Connection lost", r.name) log.Printf("%s: Connection lost", r.name)
return return
} }
if *verbose { // if *verbose {
log.Printf("\n\n%#v\n\n", r.boardstate) // log.Printf("\n\n%#v\n\n", r.boardstate)
} // }
r.recon() r.recon()
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 {
log.Println("continue")
continue continue
} }
if *verbose {
log.Printf("%s before: %+v", r.name, r.moveto)
}
r.navigate() r.navigate()
if *verbose {
log.Printf("%s after: %+v", r.name, r.moveto)
}
instruction := map[string]Instruction{ instruction := map[string]Instruction{
r.me.Id: { r.me.Id: {
@ -193,7 +206,8 @@ 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 < maxSearchIterations; i++ { var i int
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,
@ -205,6 +219,9 @@ func (r *robot) probe(destination govector.Point2d) bool {
} }
} }
} }
if *verbose {
log.Printf("%s: iterations to find destination: %d", r.name, i)
}
return true return true
} }
@ -216,21 +233,36 @@ func (r *robot) navigate() {
} }
if r.moveto == nil { if r.moveto == nil {
if *verbose {
log.Printf("%s: nil", r.name)
}
r.moveto = r.selectDirection() r.moveto = r.selectDirection()
} }
if r.me.Collision { if r.me.Collision {
if *verbose {
log.Printf("%s apparent collision", r.name)
}
r.moveto = r.selectDirection() r.moveto = r.selectDirection()
r.speed = 0 r.speed = 0
return return
} }
togo := r.me.Position.Sub(*r.moveto).Mag() togo := r.me.Position.Sub(*r.moveto).Mag()
if togo < safeDistance { if togo < safeDistance+5 {
if *verbose {
log.Printf("%s got to destination", r.name)
}
r.moveto = r.selectDirection() r.moveto = r.selectDirection()
} }
if !r.probe(r.me.Position.Add(r.me.Heading.Scale(safeDistance))) { if !r.probe(r.me.Position.Add(r.me.Heading.Scale(safeDistance))) {
if *verbose {
log.Printf("%s going to run into something", r.name)
}
r.speed = 0 r.speed = 0
if !r.probe(*r.moveto) { if !r.probe(*r.moveto) {
if *verbose {
log.Printf("%s unsafe to move, choose new direction", r.name)
}
r.moveto = r.selectDirection() r.moveto = r.selectDirection()
} }
} }