238 lines
5.0 KiB
Go
238 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bitbucket.org/hackerbots/vector"
|
|
"code.google.com/p/go.net/websocket"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"math"
|
|
"math/rand"
|
|
"sync"
|
|
)
|
|
|
|
const maxSpeed = 100
|
|
const safeDistance = 20
|
|
|
|
func connect(server string, port int) (*websocket.Conn, error) {
|
|
origin := "http://localhost/"
|
|
url := fmt.Sprintf("ws://%s:%d/ws/", server, port)
|
|
return websocket.Dial(url, "", origin)
|
|
}
|
|
|
|
type robot struct {
|
|
server string
|
|
port int
|
|
ws *websocket.Conn
|
|
game GameParam
|
|
playerId string
|
|
name string
|
|
stats StatsRequest
|
|
|
|
speed float32
|
|
moveto *govector.Point2d
|
|
fireat *govector.Point2d
|
|
boardstate Boardstate
|
|
me Robot
|
|
knownObstacles map[string]Obstacle
|
|
nearestEnemy *OtherRobot
|
|
|
|
// TODO: don't know if I like how this is done ... I would rather send
|
|
// a signal over a chanel
|
|
wg *sync.WaitGroup
|
|
}
|
|
|
|
func (r *robot) negociate() (err error) {
|
|
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))
|
|
}
|
|
|
|
err = websocket.JSON.Send(r.ws, struct {
|
|
Id string `json:"id"`
|
|
}{
|
|
r.game.name,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var idreq struct {
|
|
Type string `json:"type"`
|
|
PlayerId string `json:"id"`
|
|
}
|
|
err = websocket.JSON.Receive(r.ws, &idreq)
|
|
if err != nil || idreq.Type == "failure" {
|
|
return errors.New(fmt.Sprintf("failure: %+v", idreq))
|
|
}
|
|
log.Printf("%s: idreq: %+v", r.name, idreq)
|
|
|
|
err = websocket.JSON.Send(r.ws, struct {
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
Useragent string `json:"useragent"`
|
|
}{
|
|
Name: r.name,
|
|
Useragent: "gobot",
|
|
Type: "robot",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = websocket.JSON.Receive(r.ws, &r.game)
|
|
if r.game.Type != "gameparam" {
|
|
return errors.New("didn't receive a good gameparam")
|
|
}
|
|
log.Printf("%s: game parameters: %+v", r.name, r.game)
|
|
|
|
conf := ClientConfig{
|
|
ID: r.game.name,
|
|
Stats: map[string]StatsRequest{
|
|
r.name: r.stats,
|
|
},
|
|
}
|
|
|
|
err = websocket.JSON.Send(r.ws, conf)
|
|
|
|
var handshake struct {
|
|
Id string `json:"id"`
|
|
Success bool `json:"success"`
|
|
Type string `json:"type"`
|
|
}
|
|
websocket.JSON.Receive(r.ws, &handshake)
|
|
if !handshake.Success {
|
|
return errors.New("failed to validate correct stats request")
|
|
}
|
|
r.playerId = handshake.Id
|
|
log.Printf("%s: handshake: %+v", r.name, handshake)
|
|
return nil
|
|
}
|
|
|
|
func (r *robot) play() {
|
|
defer r.wg.Done()
|
|
var err error
|
|
err = r.negociate()
|
|
if err != nil {
|
|
log.Printf("%s: failed to negociate: %s", r.name, err)
|
|
return
|
|
}
|
|
|
|
log.Printf("%s: starting loop", r.name)
|
|
|
|
for {
|
|
r.speed = float32(maxSpeed)
|
|
err = websocket.JSON.Receive(r.ws, &r.boardstate)
|
|
if err != nil {
|
|
log.Printf("%s: Connection lost", r.name)
|
|
return
|
|
}
|
|
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 {
|
|
// XXX
|
|
log.Println("continue")
|
|
continue
|
|
}
|
|
|
|
r.navigate()
|
|
|
|
instruction := map[string]Instruction{
|
|
r.me.Id: {
|
|
MoveTo: r.moveto,
|
|
TargetSpeed: &r.speed,
|
|
FireAt: r.fireat,
|
|
},
|
|
}
|
|
err = websocket.JSON.Send(r.ws, instruction)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *robot) recon() {
|
|
for _, o := range r.boardstate.Obstacles {
|
|
if _, ok := r.knownObstacles[o.id()]; !ok {
|
|
r.knownObstacles[o.id()] = o.toObstacle()
|
|
}
|
|
}
|
|
|
|
// simplest shooting strategy ... need to do the following:
|
|
// not shoot through buildings
|
|
// shoot at where the robot will be, not where it was.
|
|
r.nearestEnemy = nil
|
|
r.fireat = nil
|
|
closest := float32(math.Inf(1))
|
|
for _, enemy := range r.boardstate.OtherRobots {
|
|
dist := r.me.Position.Sub(enemy.Position).Mag()
|
|
if dist < closest && dist > safeDistance {
|
|
r.nearestEnemy = &enemy
|
|
}
|
|
}
|
|
if r.nearestEnemy != nil {
|
|
point := r.nearestEnemy.Position.Add(r.nearestEnemy.Heading.Scale(20))
|
|
r.fireat = &point
|
|
}
|
|
}
|
|
|
|
func (r *robot) selectDirection() *govector.Point2d {
|
|
p := govector.Vector2d{
|
|
X: rand.Float32() * r.game.BoardSize.Width,
|
|
Y: rand.Float32() * r.game.BoardSize.Height,
|
|
}.ToPoint()
|
|
return &p
|
|
}
|
|
|
|
func (r *robot) probe(destination govector.Point2d) bool {
|
|
// XXX: make test for this
|
|
for i := 0; i < 20; i++ {
|
|
for _, v := range r.knownObstacles {
|
|
collided, _, _ := govector.RectIntersection(
|
|
v.Bounds,
|
|
r.me.Position,
|
|
destination.Sub(r.me.Position),
|
|
)
|
|
if collided {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (r *robot) navigate() {
|
|
if r.boardstate.Reset {
|
|
// XXX We actually *want* to get here, but never do ..
|
|
panic("reset")
|
|
r.moveto = r.selectDirection()
|
|
}
|
|
|
|
if r.moveto == nil {
|
|
r.moveto = r.selectDirection()
|
|
}
|
|
|
|
if r.me.Collision {
|
|
r.moveto = r.selectDirection()
|
|
r.speed = 0
|
|
return
|
|
}
|
|
togo := r.me.Position.Sub(*r.moveto).Mag()
|
|
if togo < 20.0 {
|
|
r.moveto = r.selectDirection()
|
|
}
|
|
if !r.probe(r.me.Position.Add(r.me.Heading.Scale(80))) {
|
|
r.speed = 0
|
|
if !r.probe(*r.moveto) {
|
|
r.moveto = r.selectDirection()
|
|
}
|
|
}
|
|
}
|