2013-09-04 23:37:39 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-20 11:16:11 -07:00
|
|
|
"bitbucket.org/hackerbots/vector"
|
2013-09-04 23:37:39 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2013-09-20 11:16:11 -07:00
|
|
|
"math/rand"
|
2013-09-04 23:37:39 -07:00
|
|
|
"net/http"
|
2013-09-28 13:10:25 -07:00
|
|
|
"time"
|
2013-09-04 23:37:39 -07:00
|
|
|
)
|
|
|
|
|
2013-10-19 00:30:20 -07:00
|
|
|
var hp = flag.Int("hp", 50, "")
|
|
|
|
var speed = flag.Float64("speed", 50, "")
|
|
|
|
var acceleration = flag.Float64("acceleration", 50, "")
|
|
|
|
var weaponRadius = flag.Int("wrad", 50, "weapon radius")
|
|
|
|
var scannerRadius = flag.Int("srad", 50, "scanner radius")
|
|
|
|
var fireRate = flag.Int("fire-rate", 50, "scanner radius")
|
|
|
|
|
2013-10-14 00:08:28 -07:00
|
|
|
var server = flag.String("server", "hackerbots.us", "server hostname")
|
2013-09-27 00:04:25 -07:00
|
|
|
var port = flag.Int("port", 8666, "server port")
|
|
|
|
var botname = flag.String("name", "gobot", "the name that other players will see")
|
2013-10-14 08:51:06 -07:00
|
|
|
var verbose = flag.Bool("verbose", false, "run verbosly")
|
2013-09-07 19:26:21 -07:00
|
|
|
|
2013-09-20 11:16:11 -07:00
|
|
|
type infos struct {
|
|
|
|
id string
|
|
|
|
width, height float64
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
type Stats struct {
|
|
|
|
Hp int `json:"hp"`
|
|
|
|
Speed float64 `json:"speed"`
|
|
|
|
Acceleration float64 `json:"acceleration"`
|
|
|
|
WeaponRadius int `json:"weapon_radius"`
|
|
|
|
ScannerRadius int `json:"scanner_radius"`
|
2013-10-19 00:30:20 -07:00
|
|
|
FireRate int `json:"fire_rate"`
|
2013-09-27 22:49:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Scanner struct {
|
|
|
|
Position govector.Point2d `json:"position"`
|
|
|
|
Stats Stats `json:"stats"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Robot struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Stats Stats `json:"stats"`
|
|
|
|
TargetSpeed float64 `json:"speed"`
|
|
|
|
Speed float64 `json:"speed"`
|
|
|
|
Health int `json:"health"`
|
|
|
|
Position govector.Point2d `json:"position"`
|
|
|
|
Heading govector.Vector2d `json:"heading"`
|
|
|
|
MoveTo *govector.Point2d `json:"move_to,omitempty"`
|
|
|
|
FireAt *govector.Point2d `json:"fire_at,omitempty"`
|
|
|
|
Scanners []Scanner `json:"scanners"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Projectile struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Position govector.Point2d `json:"position"`
|
|
|
|
MoveTo govector.Point2d `json:"move_to"`
|
|
|
|
Radius int `json:"radius"`
|
|
|
|
Speed float64 `json:"speed"`
|
|
|
|
Damage int `json:"damage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Splosion struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Position govector.Point2d `json:"position"`
|
|
|
|
Radius int `json:"radius"`
|
|
|
|
MaxDamage int `json:"damage"`
|
|
|
|
MinDamage int `json:"damage"`
|
|
|
|
Lifespan int `json:"lifespan"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Instruction struct {
|
|
|
|
MoveTo *govector.Point2d `json:"move_to,omitempty"`
|
|
|
|
FireAt *govector.Point2d `json:"fire_at,omitempty"`
|
|
|
|
Stats Stats `json:"stats"`
|
|
|
|
}
|
|
|
|
|
2013-09-04 23:37:39 -07:00
|
|
|
func connect() (*websocket.Conn, error) {
|
|
|
|
origin := "http://localhost/"
|
2013-09-27 00:04:25 -07:00
|
|
|
url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port)
|
2013-09-04 23:37:39 -07:00
|
|
|
return websocket.Dial(url, "", origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createGame() (string, error) {
|
2013-09-28 14:33:51 -07:00
|
|
|
url := fmt.Sprintf("http://%s:%d/game/start/", *server, *port)
|
|
|
|
resp, err := http.Get(url)
|
2013-09-04 23:37:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var g struct {
|
2013-09-04 23:39:24 -07:00
|
|
|
Id string `json:"id"`
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&g); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return g.Id, err
|
|
|
|
}
|
|
|
|
|
2013-09-20 11:16:11 -07:00
|
|
|
func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) {
|
2013-09-04 23:37:39 -07:00
|
|
|
log.Printf("trying to connect to game %s", gameid)
|
2013-09-27 22:49:05 -07:00
|
|
|
err = websocket.JSON.Send(ws, struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
2013-09-04 23:37:39 -07:00
|
|
|
gameid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-09-20 11:16:11 -07:00
|
|
|
return nil, err
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
var idreq struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
AssignedID string `json:"id"`
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
err = websocket.JSON.Receive(ws, &idreq)
|
|
|
|
if err != nil || idreq.Type == "failure" {
|
2013-09-20 11:16:11 -07:00
|
|
|
return nil, errors.New(fmt.Sprintf("failure: %+v", idreq))
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
log.Printf("%+v", idreq)
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
err = websocket.JSON.Send(ws, struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Useragent string `json:"useragent"`
|
|
|
|
}{
|
2013-09-27 00:04:25 -07:00
|
|
|
Name: *botname,
|
2013-09-04 23:37:39 -07:00
|
|
|
Useragent: "gobot",
|
|
|
|
Type: "robot",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-09-20 11:16:11 -07:00
|
|
|
return nil, err
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
type BoardSize struct {
|
|
|
|
Width float64 `json:"width"`
|
|
|
|
Height float64 `json:"height"`
|
|
|
|
}
|
|
|
|
var gameparam struct {
|
|
|
|
BoardSize BoardSize `json:"boardsize"`
|
|
|
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
err = websocket.JSON.Receive(ws, &gameparam)
|
|
|
|
log.Printf("gameparam: %+v", gameparam)
|
|
|
|
if gameparam.Type != "gameparam" {
|
2013-09-20 11:16:11 -07:00
|
|
|
return nil, errors.New("didn't receive a good gameparam")
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
log.Printf("%+v", gameparam)
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
err = websocket.JSON.Send(ws, &struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
// TODO: candidate for embedding?
|
|
|
|
Stats Stats `json:"stats"`
|
|
|
|
}{
|
2013-09-27 00:04:25 -07:00
|
|
|
ID: gameid,
|
|
|
|
Name: *botname,
|
2013-09-27 22:49:05 -07:00
|
|
|
Stats: Stats{
|
2013-09-07 19:26:21 -07:00
|
|
|
Hp: *hp,
|
|
|
|
Speed: *speed,
|
2013-09-28 09:44:24 -07:00
|
|
|
Acceleration: *acceleration,
|
2013-09-07 19:26:21 -07:00
|
|
|
WeaponRadius: *weaponRadius,
|
|
|
|
ScannerRadius: *scannerRadius,
|
2013-10-19 00:30:20 -07:00
|
|
|
FireRate: *fireRate,
|
2013-09-04 23:37:39 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
var handshake struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
websocket.JSON.Receive(ws, &handshake)
|
|
|
|
if !handshake.Success {
|
2013-09-20 11:16:11 -07:00
|
|
|
return nil, errors.New("failed to validate correct stats request")
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
log.Printf("%+v", handshake)
|
2013-09-20 11:16:11 -07:00
|
|
|
return &infos{
|
|
|
|
id: idreq.AssignedID,
|
|
|
|
width: gameparam.BoardSize.Width,
|
|
|
|
height: gameparam.BoardSize.Height,
|
|
|
|
}, err
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2013-09-28 13:10:25 -07:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2013-09-04 23:37:39 -07:00
|
|
|
var err error
|
|
|
|
var gameid string
|
|
|
|
flag.Parse()
|
|
|
|
if flag.NArg() < 1 {
|
|
|
|
gameid, err = createGame()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gameid = flag.Arg(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := connect()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-09-20 11:16:11 -07:00
|
|
|
board_info, err := negociate(ws, gameid)
|
|
|
|
if err != nil {
|
2013-09-04 23:37:39 -07:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-09-27 00:04:25 -07:00
|
|
|
// TODO: var target govector.Point2d
|
2013-09-20 11:16:11 -07:00
|
|
|
moveto := govector.Point2d{
|
|
|
|
X: rand.Float64() * board_info.width,
|
|
|
|
Y: rand.Float64() * board_info.height,
|
|
|
|
}
|
|
|
|
log.Printf("%+v", moveto)
|
|
|
|
|
2013-09-27 22:49:05 -07:00
|
|
|
var me Robot
|
2013-09-20 11:16:11 -07:00
|
|
|
|
2013-09-27 00:04:25 -07:00
|
|
|
log.Printf("negociated well, starting loop")
|
2013-09-04 23:37:39 -07:00
|
|
|
for {
|
2013-09-27 22:49:05 -07:00
|
|
|
var boardstate struct {
|
|
|
|
Robots []Robot `json:"robots"`
|
|
|
|
Projectiles []Projectile `json:"projectiles"`
|
|
|
|
Splosions []Splosion `json:"splosions"`
|
|
|
|
Reset bool `json:"reset"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Turn int `json:"turn"`
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
err = websocket.JSON.Receive(ws, &boardstate)
|
2013-10-14 08:51:06 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("%+v", boardstate)
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Connection lost")
|
|
|
|
}
|
2013-09-20 11:16:11 -07:00
|
|
|
for _, bot := range boardstate.Robots {
|
|
|
|
if bot.Id == board_info.id {
|
|
|
|
me = bot
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if govector.Distance(me.Position, moveto) < 3.0 {
|
2013-09-28 13:10:25 -07:00
|
|
|
log.Printf("old: %+v: %+v", me.Position, moveto)
|
2013-09-20 11:16:11 -07:00
|
|
|
moveto = govector.Point2d{
|
|
|
|
X: rand.Float64() * board_info.width,
|
|
|
|
Y: rand.Float64() * board_info.height,
|
|
|
|
}
|
2013-09-28 13:10:25 -07:00
|
|
|
log.Printf("new: %+v: %+v", me.Position, moveto)
|
2013-09-20 11:16:11 -07:00
|
|
|
}
|
|
|
|
|
2013-09-04 23:37:39 -07:00
|
|
|
// TODO: send instructions
|
2013-09-27 22:49:05 -07:00
|
|
|
err = websocket.JSON.Send(ws, Instruction{
|
2013-09-20 11:16:11 -07:00
|
|
|
MoveTo: &moveto,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-09-04 23:37:39 -07:00
|
|
|
}
|
|
|
|
}
|