From 476e2a3ff89daf49e5c9cd52e8def1b41b053009 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Fri, 20 Sep 2013 12:16:11 -0600 Subject: [PATCH] got bot moving to random locations --- main.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 6bd3906..2881bb6 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,14 @@ package main import ( "bitbucket.org/hackerbots/bot" + "bitbucket.org/hackerbots/vector" "code.google.com/p/go.net/websocket" "encoding/json" "errors" "flag" "fmt" "log" + "math/rand" "net/http" ) @@ -16,6 +18,11 @@ var speed = flag.Float64("speed", 150, "") var weaponRadius = flag.Int("wrad", 100, "weapon radius") var scannerRadius = flag.Int("srad", 100, "scanner radius") +type infos struct { + id string + width, height float64 +} + func connect() (*websocket.Conn, error) { origin := "http://localhost/" url := "ws://localhost:8666/ws/" @@ -38,19 +45,19 @@ func createGame() (string, error) { return g.Id, err } -func negociate(ws *websocket.Conn, gameid string) (err error) { +func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) { log.Printf("trying to connect to game %s", gameid) err = websocket.JSON.Send(ws, bot.GameID{ gameid, }) if err != nil { - return err + return nil, err } var idreq bot.IdRequest err = websocket.JSON.Receive(ws, &idreq) if err != nil || idreq.Type == "failure" { - return errors.New(fmt.Sprintf("failure: %+v", idreq)) + return nil, errors.New(fmt.Sprintf("failure: %+v", idreq)) } log.Printf("%+v", idreq) @@ -60,14 +67,14 @@ func negociate(ws *websocket.Conn, gameid string) (err error) { Type: "robot", }) if err != nil { - return err + return nil, err } var gameparam bot.GameParam err = websocket.JSON.Receive(ws, &gameparam) log.Printf("gameparam: %+v", gameparam) if gameparam.Type != "gameparam" { - return errors.New("didn't receive a good gameparam") + return nil, errors.New("didn't receive a good gameparam") } log.Printf("%+v", gameparam) @@ -84,10 +91,14 @@ func negociate(ws *websocket.Conn, gameid string) (err error) { var handshake bot.Handshake websocket.JSON.Receive(ws, &handshake) if !handshake.Success { - return errors.New("failed to validate correct stats request") + return nil, errors.New("failed to validate correct stats request") } log.Printf("%+v", handshake) - return err + return &infos{ + id: idreq.AssignedID, + width: gameparam.BoardSize.Width, + height: gameparam.BoardSize.Height, + }, err } func main() { @@ -107,18 +118,47 @@ func main() { if err != nil { log.Fatal(err) } - if err = negociate(ws, gameid); err != nil { + board_info, err := negociate(ws, gameid) + if err != nil { log.Fatal(err) } + moveto := govector.Point2d{ + X: rand.Float64() * board_info.width, + Y: rand.Float64() * board_info.height, + } + log.Printf("%+v", moveto) + + var me bot.Robot + for { var boardstate bot.Boardstate err = websocket.JSON.Receive(ws, &boardstate) if err != nil { log.Fatal("Connection lost") } - // log.Printf("%+v", boardstate) - // TODO: logic + for _, bot := range boardstate.Robots { + if bot.Id == board_info.id { + me = bot + break + } + } + log.Printf("%+v: %+v", me.Position, moveto) + // come up with new location if I am there + if govector.Distance(me.Position, moveto) < 3.0 { + log.Println("finally got there") + moveto = govector.Point2d{ + X: rand.Float64() * board_info.width, + Y: rand.Float64() * board_info.height, + } + } + // TODO: send instructions + err = websocket.JSON.Send(ws, bot.Instruction{ + MoveTo: &moveto, + }) + if err != nil { + log.Fatal(err) + } } }