got bot moving to random locations

This commit is contained in:
Stephen McQuay 2013-09-20 12:16:11 -06:00
parent 2ad28180ee
commit 476e2a3ff8

60
main.go
View File

@ -2,12 +2,14 @@ package main
import ( import (
"bitbucket.org/hackerbots/bot" "bitbucket.org/hackerbots/bot"
"bitbucket.org/hackerbots/vector"
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
"encoding/json" "encoding/json"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"math/rand"
"net/http" "net/http"
) )
@ -16,6 +18,11 @@ var speed = flag.Float64("speed", 150, "")
var weaponRadius = flag.Int("wrad", 100, "weapon radius") var weaponRadius = flag.Int("wrad", 100, "weapon radius")
var scannerRadius = flag.Int("srad", 100, "scanner radius") var scannerRadius = flag.Int("srad", 100, "scanner radius")
type infos struct {
id string
width, height float64
}
func connect() (*websocket.Conn, error) { func connect() (*websocket.Conn, error) {
origin := "http://localhost/" origin := "http://localhost/"
url := "ws://localhost:8666/ws/" url := "ws://localhost:8666/ws/"
@ -38,19 +45,19 @@ func createGame() (string, error) {
return g.Id, err 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) log.Printf("trying to connect to game %s", gameid)
err = websocket.JSON.Send(ws, bot.GameID{ err = websocket.JSON.Send(ws, bot.GameID{
gameid, gameid,
}) })
if err != nil { if err != nil {
return err return nil, err
} }
var idreq bot.IdRequest var idreq bot.IdRequest
err = websocket.JSON.Receive(ws, &idreq) err = websocket.JSON.Receive(ws, &idreq)
if err != nil || idreq.Type == "failure" { 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) log.Printf("%+v", idreq)
@ -60,14 +67,14 @@ func negociate(ws *websocket.Conn, gameid string) (err error) {
Type: "robot", Type: "robot",
}) })
if err != nil { if err != nil {
return err return nil, err
} }
var gameparam bot.GameParam var gameparam bot.GameParam
err = websocket.JSON.Receive(ws, &gameparam) err = websocket.JSON.Receive(ws, &gameparam)
log.Printf("gameparam: %+v", gameparam) log.Printf("gameparam: %+v", gameparam)
if gameparam.Type != "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) log.Printf("%+v", gameparam)
@ -84,10 +91,14 @@ func negociate(ws *websocket.Conn, gameid string) (err error) {
var handshake bot.Handshake var handshake bot.Handshake
websocket.JSON.Receive(ws, &handshake) websocket.JSON.Receive(ws, &handshake)
if !handshake.Success { 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) log.Printf("%+v", handshake)
return err return &infos{
id: idreq.AssignedID,
width: gameparam.BoardSize.Width,
height: gameparam.BoardSize.Height,
}, err
} }
func main() { func main() {
@ -107,18 +118,47 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err = negociate(ws, gameid); err != nil { board_info, err := negociate(ws, gameid)
if err != nil {
log.Fatal(err) 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 { for {
var boardstate bot.Boardstate var boardstate bot.Boardstate
err = websocket.JSON.Receive(ws, &boardstate) err = websocket.JSON.Receive(ws, &boardstate)
if err != nil { if err != nil {
log.Fatal("Connection lost") log.Fatal("Connection lost")
} }
// log.Printf("%+v", boardstate) for _, bot := range boardstate.Robots {
// TODO: logic 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 // TODO: send instructions
err = websocket.JSON.Send(ws, bot.Instruction{
MoveTo: &moveto,
})
if err != nil {
log.Fatal(err)
}
} }
} }