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
1 changed files with 50 additions and 10 deletions

60
main.go
View File

@ -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)
}
}
}