2013-11-10 23:30:23 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-01-11 00:08:08 -08:00
|
|
|
"math"
|
2013-11-10 23:30:23 -08:00
|
|
|
"math/rand"
|
2013-11-11 00:25:28 -08:00
|
|
|
"sync"
|
2014-03-10 00:04:45 -07:00
|
|
|
|
|
|
|
"bitbucket.org/hackerbots/vector"
|
|
|
|
"code.google.com/p/go.net/websocket"
|
2013-11-10 23:30:23 -08:00
|
|
|
)
|
|
|
|
|
2014-01-10 01:22:33 -08:00
|
|
|
const maxSpeed = 100
|
2014-01-11 23:14:44 -08:00
|
|
|
const safeDistance = 40
|
|
|
|
const maxSearchIterations = 20
|
2014-01-10 01:22:33 -08:00
|
|
|
|
2013-11-11 00:01:57 -08:00
|
|
|
func connect(server string, port int) (*websocket.Conn, error) {
|
2013-11-10 23:30:23 -08:00
|
|
|
origin := "http://localhost/"
|
2013-11-11 00:01:57 -08:00
|
|
|
url := fmt.Sprintf("ws://%s:%d/ws/", server, port)
|
2013-11-10 23:30:23 -08:00
|
|
|
return websocket.Dial(url, "", origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
type robot struct {
|
2014-01-23 22:33:05 -08:00
|
|
|
server string
|
|
|
|
port int
|
|
|
|
ws *websocket.Conn
|
|
|
|
game GameParam
|
|
|
|
playerId string
|
|
|
|
name string
|
|
|
|
statsReq StatsRequest
|
|
|
|
statsCalculated Stats
|
2013-11-11 00:25:28 -08:00
|
|
|
|
2014-01-10 01:22:33 -08:00
|
|
|
speed float32
|
|
|
|
moveto *govector.Point2d
|
2014-01-11 00:08:08 -08:00
|
|
|
fireat *govector.Point2d
|
2014-01-10 01:22:33 -08:00
|
|
|
boardstate Boardstate
|
|
|
|
me Robot
|
|
|
|
knownObstacles map[string]Obstacle
|
2014-01-11 00:08:08 -08:00
|
|
|
nearestEnemy *OtherRobot
|
2014-01-10 01:22:33 -08:00
|
|
|
|
2013-11-11 00:25:28 -08:00
|
|
|
// TODO: don't know if I like how this is done ... I would rather send
|
|
|
|
// a signal over a chanel
|
|
|
|
wg *sync.WaitGroup
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:01:57 -08:00
|
|
|
func (r *robot) negociate() (err error) {
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
2014-01-22 22:14:02 -08:00
|
|
|
log.Printf("%s: trying to connect to game '%s'", r.name, r.game.Name)
|
2014-01-12 00:03:17 -08:00
|
|
|
}
|
2013-11-11 00:01:57 -08:00
|
|
|
r.ws, err = connect(r.server, r.port)
|
2013-11-10 23:30:23 -08:00
|
|
|
if err != nil {
|
2013-11-11 00:01:57 -08:00
|
|
|
return errors.New(fmt.Sprintf("connection failure: %s", err))
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(r.ws, struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
2014-01-22 22:14:02 -08:00
|
|
|
r.game.Name,
|
2013-11-10 23:30:23 -08:00
|
|
|
})
|
|
|
|
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))
|
|
|
|
}
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s: idreq: %+v", r.name, idreq)
|
|
|
|
}
|
2013-11-10 23:30:23 -08:00
|
|
|
|
|
|
|
err = websocket.JSON.Send(r.ws, struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Useragent string `json:"useragent"`
|
|
|
|
}{
|
2013-11-10 23:37:43 -08:00
|
|
|
Name: r.name,
|
2013-11-10 23:30:23 -08:00
|
|
|
Useragent: "gobot",
|
|
|
|
Type: "robot",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-10 00:04:45 -07:00
|
|
|
|
2014-03-10 21:52:14 -07:00
|
|
|
supportedEncs := []string{"bson", "json", "gob"}
|
|
|
|
err = websocket.JSON.Send(r.ws, supportedEncs)
|
2014-03-10 00:04:45 -07:00
|
|
|
if err != nil {
|
2014-03-10 21:52:14 -07:00
|
|
|
return errors.New(fmt.Sprintf("failure: %+v", err))
|
2014-03-10 00:04:45 -07:00
|
|
|
}
|
|
|
|
|
2013-11-10 23:30:23 -08:00
|
|
|
err = websocket.JSON.Receive(r.ws, &r.game)
|
|
|
|
if r.game.Type != "gameparam" {
|
|
|
|
return errors.New("didn't receive a good gameparam")
|
|
|
|
}
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s: game parameters: %+v", r.name, r.game)
|
|
|
|
}
|
2013-11-10 23:30:23 -08:00
|
|
|
|
|
|
|
conf := ClientConfig{
|
2014-01-22 22:14:02 -08:00
|
|
|
ID: r.game.Name,
|
2013-11-10 23:30:23 -08:00
|
|
|
Stats: map[string]StatsRequest{
|
2014-01-23 22:33:05 -08:00
|
|
|
r.name: r.statsReq,
|
2013-11-10 23:30:23 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(r.ws, conf)
|
|
|
|
|
|
|
|
var handshake struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Type string `json:"type"`
|
2014-01-23 22:33:05 -08:00
|
|
|
Failure
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
websocket.JSON.Receive(r.ws, &handshake)
|
|
|
|
if !handshake.Success {
|
2014-01-23 22:33:05 -08:00
|
|
|
return errors.New(handshake.Reason)
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
r.playerId = handshake.Id
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s: handshake: %+v", r.name, handshake)
|
|
|
|
}
|
2014-01-23 22:33:05 -08:00
|
|
|
|
2014-01-23 23:03:21 -08:00
|
|
|
dstats := struct {
|
|
|
|
Stats map[string]Stats `json:"stats"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{}
|
2014-01-23 22:33:05 -08:00
|
|
|
err = websocket.JSON.Receive(r.ws, &dstats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-29 22:46:31 -08:00
|
|
|
log.Printf("%s: recv stats", r.name)
|
2014-01-23 22:33:05 -08:00
|
|
|
|
|
|
|
// this player only ever has one robot, so we're just picking off our own
|
|
|
|
// stats
|
2014-01-23 23:03:21 -08:00
|
|
|
_, ok := dstats.Stats[r.name]
|
2014-01-23 22:33:05 -08:00
|
|
|
if !ok {
|
|
|
|
return errors.New("my name not found in stats map")
|
|
|
|
}
|
2014-01-23 23:03:21 -08:00
|
|
|
r.statsCalculated = dstats.Stats[r.name]
|
2014-01-23 22:33:05 -08:00
|
|
|
|
2013-11-10 23:30:23 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robot) play() {
|
2013-11-11 00:25:28 -08:00
|
|
|
defer r.wg.Done()
|
2013-11-10 23:30:23 -08:00
|
|
|
var err error
|
2013-11-11 00:01:57 -08:00
|
|
|
err = r.negociate()
|
|
|
|
if err != nil {
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: failed to negociate: %s", r.name, err)
|
|
|
|
return
|
2013-11-11 00:01:57 -08:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:25:02 -08:00
|
|
|
log.Printf("%s: starting loop", r.name)
|
2014-01-10 01:22:33 -08:00
|
|
|
|
2013-11-10 23:30:23 -08:00
|
|
|
for {
|
2014-01-10 01:22:33 -08:00
|
|
|
r.speed = float32(maxSpeed)
|
2014-01-29 22:46:31 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s: about to wait on boardstate", r.name)
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
err = websocket.JSON.Receive(r.ws, &r.boardstate)
|
2014-01-29 22:46:31 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s: one recv boardstate", r.name)
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s: Connection lost", r.name)
|
|
|
|
return
|
|
|
|
}
|
2014-01-29 22:46:31 -08:00
|
|
|
// TODO: I need a truly-verbose flag?
|
2014-01-12 00:03:17 -08:00
|
|
|
// if *verbose {
|
|
|
|
// log.Printf("\n\n%#v\n\n", r.boardstate)
|
|
|
|
// }
|
2014-01-10 01:22:33 -08:00
|
|
|
|
|
|
|
r.recon()
|
|
|
|
|
|
|
|
if len(r.boardstate.MyRobots) > 0 {
|
|
|
|
r.me = r.boardstate.MyRobots[0]
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s before: %+v", r.name, r.moveto)
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
r.navigate()
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s after: %+v", r.name, r.moveto)
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
|
|
|
|
instruction := map[string]Instruction{
|
|
|
|
r.me.Id: {
|
|
|
|
MoveTo: r.moveto,
|
|
|
|
TargetSpeed: &r.speed,
|
2014-01-11 00:08:08 -08:00
|
|
|
FireAt: r.fireat,
|
2014-01-10 01:22:33 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = websocket.JSON.Send(r.ws, instruction)
|
2013-11-10 23:30:23 -08:00
|
|
|
if err != nil {
|
2014-01-10 01:22:33 -08:00
|
|
|
log.Println(err)
|
2013-11-11 00:25:02 -08:00
|
|
|
return
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
|
|
|
}
|
2013-11-10 23:30:23 -08:00
|
|
|
|
2014-01-10 01:22:33 -08:00
|
|
|
func (r *robot) recon() {
|
|
|
|
for _, o := range r.boardstate.Obstacles {
|
|
|
|
if _, ok := r.knownObstacles[o.id()]; !ok {
|
|
|
|
r.knownObstacles[o.id()] = o.toObstacle()
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 00:08:08 -08:00
|
|
|
|
|
|
|
// 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 {
|
2014-01-11 23:14:44 -08:00
|
|
|
point := r.nearestEnemy.Position.Add(r.nearestEnemy.Heading.Scale(safeDistance))
|
2014-01-11 00:08:08 -08:00
|
|
|
r.fireat = &point
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
|
|
|
|
2014-01-12 01:27:12 -08:00
|
|
|
func (r *robot) randomDirection() *govector.Point2d {
|
2014-01-10 01:22:33 -08:00
|
|
|
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
|
2014-01-12 01:23:20 -08:00
|
|
|
for _, v := range r.knownObstacles {
|
|
|
|
collided, _, _ := govector.RectIntersection(
|
|
|
|
v.Bounds,
|
|
|
|
r.me.Position,
|
|
|
|
destination.Sub(r.me.Position),
|
|
|
|
)
|
|
|
|
if collided {
|
|
|
|
return false
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robot) navigate() {
|
|
|
|
if r.boardstate.Reset {
|
|
|
|
// XXX We actually *want* to get here, but never do ..
|
|
|
|
panic("reset")
|
2014-01-12 01:27:12 -08:00
|
|
|
r.moveto = r.randomDirection()
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.moveto == nil {
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s: nil", r.name)
|
|
|
|
}
|
2014-01-12 01:27:12 -08:00
|
|
|
r.moveto = r.randomDirection()
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
togo := r.me.Position.Sub(*r.moveto).Mag()
|
2014-01-12 00:03:17 -08:00
|
|
|
if togo < safeDistance+5 {
|
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s got to destination", r.name)
|
|
|
|
}
|
2014-01-12 01:27:12 -08:00
|
|
|
r.moveto = r.randomDirection()
|
2014-01-12 01:23:20 -08:00
|
|
|
return
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
2014-01-11 23:14:44 -08:00
|
|
|
if !r.probe(r.me.Position.Add(r.me.Heading.Scale(safeDistance))) {
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s going to run into something", r.name)
|
|
|
|
}
|
2014-01-10 01:22:33 -08:00
|
|
|
r.speed = 0
|
|
|
|
if !r.probe(*r.moveto) {
|
2014-01-12 00:03:17 -08:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%s unsafe to move, choose new direction", r.name)
|
|
|
|
}
|
2014-01-12 01:27:12 -08:00
|
|
|
r.moveto = r.randomDirection()
|
2014-01-12 01:23:20 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 20:39:08 -08:00
|
|
|
if r.me.Collision != nil {
|
2014-01-12 01:23:20 -08:00
|
|
|
// XXX: I am being told I am here ...
|
|
|
|
if *verbose {
|
2014-01-27 20:39:08 -08:00
|
|
|
log.Printf("%s apparent collision: %#v", r.name, r.me.Collision)
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
2014-01-12 01:27:12 -08:00
|
|
|
r.moveto = r.randomDirection()
|
2014-01-12 01:23:20 -08:00
|
|
|
r.speed = 0
|
|
|
|
return
|
2014-01-10 01:22:33 -08:00
|
|
|
}
|
2013-11-10 23:30:23 -08:00
|
|
|
}
|