From 5b8ec0b8abe4a492750ec935afbd247cd7d867fb Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sun, 12 Jan 2014 00:03:17 -0800 Subject: [PATCH] consider destination slightly before collision also: added verbose blocks for logging --- robot.go | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/robot.go b/robot.go index 667b5da..bd80980 100644 --- a/robot.go +++ b/robot.go @@ -44,7 +44,9 @@ type robot struct { } func (r *robot) negociate() (err error) { - log.Printf("%s: trying to connect to game '%s'", r.name, r.game.name) + if *verbose { + log.Printf("%s: trying to connect to game '%s'", r.name, r.game.name) + } r.ws, err = connect(r.server, r.port) if err != nil { 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" { return errors.New(fmt.Sprintf("failure: %+v", idreq)) } - log.Printf("%s: idreq: %+v", r.name, idreq) + if *verbose { + log.Printf("%s: idreq: %+v", r.name, idreq) + } err = websocket.JSON.Send(r.ws, struct { Type string `json:"type"` @@ -85,7 +89,9 @@ func (r *robot) negociate() (err error) { if r.game.Type != "gameparam" { return errors.New("didn't receive a good gameparam") } - log.Printf("%s: game parameters: %+v", r.name, r.game) + if *verbose { + log.Printf("%s: game parameters: %+v", r.name, r.game) + } conf := ClientConfig{ ID: r.game.name, @@ -106,7 +112,9 @@ func (r *robot) negociate() (err error) { return errors.New("failed to validate correct stats request") } r.playerId = handshake.Id - log.Printf("%s: handshake: %+v", r.name, handshake) + if *verbose { + log.Printf("%s: handshake: %+v", r.name, handshake) + } return nil } @@ -128,20 +136,25 @@ func (r *robot) play() { log.Printf("%s: Connection lost", r.name) return } - if *verbose { - log.Printf("\n\n%#v\n\n", r.boardstate) - } + // if *verbose { + // log.Printf("\n\n%#v\n\n", r.boardstate) + // } r.recon() if len(r.boardstate.MyRobots) > 0 { r.me = r.boardstate.MyRobots[0] } else { - log.Println("continue") continue } + if *verbose { + log.Printf("%s before: %+v", r.name, r.moveto) + } r.navigate() + if *verbose { + log.Printf("%s after: %+v", r.name, r.moveto) + } instruction := map[string]Instruction{ r.me.Id: { @@ -193,7 +206,8 @@ func (r *robot) selectDirection() *govector.Point2d { func (r *robot) probe(destination govector.Point2d) bool { // 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 { collided, _, _ := govector.RectIntersection( 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 } @@ -216,21 +233,36 @@ func (r *robot) navigate() { } if r.moveto == nil { + if *verbose { + log.Printf("%s: nil", r.name) + } r.moveto = r.selectDirection() } if r.me.Collision { + if *verbose { + log.Printf("%s apparent collision", r.name) + } r.moveto = r.selectDirection() r.speed = 0 return } 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() } 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 if !r.probe(*r.moveto) { + if *verbose { + log.Printf("%s unsafe to move, choose new direction", r.name) + } r.moveto = r.selectDirection() } }