got bot moving and shooting again
This commit is contained in:
parent
42ba19dd1c
commit
35dc411a8a
120
botserv.go
Normal file
120
botserv.go
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bitbucket.org/hackerbots/vector"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ClientConfig struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Stats map[string]StatsRequest `json:"stats"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StatsRequest struct {
|
||||||
|
Hp int `json:"hp"`
|
||||||
|
Speed int `json:"speed"`
|
||||||
|
Acceleration int `json:"acceleration"`
|
||||||
|
WeaponRadius int `json:"weapon_radius"`
|
||||||
|
ScannerRadius int `json:"scanner_radius"`
|
||||||
|
TurnSpeed int `json:"turn_speed"`
|
||||||
|
FireRate int `json:"fire_rate"`
|
||||||
|
WeaponDamage int `json:"weapon_damage"`
|
||||||
|
WeaponSpeed int `json:"weapon_speed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Scanner struct {
|
||||||
|
Position govector.Point2d `json:"position"`
|
||||||
|
Stats StatsRequest `json:"stats"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Robot struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Message string `json:"-"`
|
||||||
|
Stats Stats `json:"-"`
|
||||||
|
TargetSpeed float32 `json:"-"`
|
||||||
|
Speed float32 `json:"speed"`
|
||||||
|
Health int `json:"health"`
|
||||||
|
RepairCounter float32 `json:"repair"`
|
||||||
|
ScanCounter float32 `json:"scan_bonus"`
|
||||||
|
ActiveScan bool `json:"-"`
|
||||||
|
Position govector.Point2d `json:"position"`
|
||||||
|
Heading govector.Vector2d `json:"heading"`
|
||||||
|
MoveTo *govector.Point2d `json:"-"`
|
||||||
|
FireAt *govector.Point2d `json:"-"`
|
||||||
|
Scanners []Scanner `json:"scanners"`
|
||||||
|
LastFired int `json:"-"`
|
||||||
|
Collision bool `json:"collision"`
|
||||||
|
Hit bool `json:"hit"`
|
||||||
|
Probe *govector.Point2d `json:"probe"`
|
||||||
|
ProbeResult *govector.Point2d `json:"probe_result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stats struct {
|
||||||
|
Hp int `json:"-"`
|
||||||
|
Speed float32 `json:"-"`
|
||||||
|
Acceleration float32 `json:"-"`
|
||||||
|
WeaponRadius int `json:"-"`
|
||||||
|
ScannerRadius int `json:"-"`
|
||||||
|
TurnSpeed int `json:"-"`
|
||||||
|
FireRate int `json:"-"`
|
||||||
|
WeaponDamage int `json:"-"`
|
||||||
|
WeaponSpeed float32 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Projectile struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Position govector.Point2d `json:"position"`
|
||||||
|
MoveTo govector.Point2d `json:"move_to"`
|
||||||
|
Radius int `json:"radius"`
|
||||||
|
Speed float32 `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 {
|
||||||
|
Message *string `json:"message,omitempty"`
|
||||||
|
MoveTo *govector.Point2d `json:"move_to,omitempty"`
|
||||||
|
FireAt *govector.Point2d `json:"fire_at,omitempty"`
|
||||||
|
Probe *govector.Point2d `json:"probe,omitempty"`
|
||||||
|
TargetSpeed *float32 `json:"target_speed,omitempty"`
|
||||||
|
Repair *bool `json:"repair,omitempty"`
|
||||||
|
Scan *bool `json:"scan,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Boardstate struct {
|
||||||
|
MyRobots []Robot `json:"my_robots"`
|
||||||
|
OtherRobots []OtherRobot `json:"robots"`
|
||||||
|
Projectiles []Projectile `json:"projectiles"`
|
||||||
|
Splosions []Splosion `json:"splosions"`
|
||||||
|
Obstacles []Obstacle `json:"obj"`
|
||||||
|
Reset bool `json:"reset"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Turn int `json:"turn"`
|
||||||
|
AllBots []BotHealth `json:"all_bots"`
|
||||||
|
Messages []string `json:"messages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OtherRobot struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Position govector.Point2d `json:"position"`
|
||||||
|
Heading govector.Vector2d `json:"heading"`
|
||||||
|
Health int `json:"health"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Obstacle struct {
|
||||||
|
Bounds govector.Rect2d `json:"bounds"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BotHealth struct {
|
||||||
|
RobotId string `json:"robot_id"`
|
||||||
|
Health int `json:"health"`
|
||||||
|
}
|
134
main.go
134
main.go
@ -3,13 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bitbucket.org/hackerbots/vector"
|
"bitbucket.org/hackerbots/vector"
|
||||||
"code.google.com/p/go.net/websocket"
|
"code.google.com/p/go.net/websocket"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,89 +28,12 @@ type infos struct {
|
|||||||
width, height float32
|
width, height float32
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConfig struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Stats map[string]StatsRequest `json:"stats"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type StatsRequest struct {
|
|
||||||
Hp int `json:"hp"`
|
|
||||||
Speed int `json:"speed"`
|
|
||||||
Acceleration int `json:"acceleration"`
|
|
||||||
WeaponRadius int `json:"weapon_radius"`
|
|
||||||
ScannerRadius int `json:"scanner_radius"`
|
|
||||||
TurnSpeed int `json:"turn_speed"`
|
|
||||||
FireRate int `json:"fire_rate"`
|
|
||||||
WeaponDamage int `json:"weapon_damage"`
|
|
||||||
WeaponSpeed int `json:"weapon_speed"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Scanner struct {
|
|
||||||
Position govector.Point2d `json:"position"`
|
|
||||||
Stats StatsRequest `json:"stats"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Robot struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Stats StatsRequest `json:"stats"`
|
|
||||||
TargetSpeed float32 `json:"speed"`
|
|
||||||
Speed float32 `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 float32 `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 StatsRequest `json:"stats"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func connect() (*websocket.Conn, error) {
|
func connect() (*websocket.Conn, error) {
|
||||||
origin := "http://localhost/"
|
origin := "http://localhost/"
|
||||||
url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port)
|
url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port)
|
||||||
return websocket.Dial(url, "", origin)
|
return websocket.Dial(url, "", origin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createGame() (string, error) {
|
|
||||||
url := fmt.Sprintf("http://%s:%d/game/start/", *server, *port)
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
var g struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
}
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&g); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return g.Id, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func negociate(ws *websocket.Conn, gameid string) (i *infos, 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, struct {
|
err = websocket.JSON.Send(ws, struct {
|
||||||
@ -132,7 +53,7 @@ func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) {
|
|||||||
if err != nil || idreq.Type == "failure" {
|
if err != nil || idreq.Type == "failure" {
|
||||||
return nil, errors.New(fmt.Sprintf("failure: %+v", idreq))
|
return nil, errors.New(fmt.Sprintf("failure: %+v", idreq))
|
||||||
}
|
}
|
||||||
log.Printf("%+v", idreq)
|
log.Printf("idreq: %+v", idreq)
|
||||||
|
|
||||||
err = websocket.JSON.Send(ws, struct {
|
err = websocket.JSON.Send(ws, struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
@ -157,11 +78,10 @@ func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
err = websocket.JSON.Receive(ws, &gameparam)
|
err = websocket.JSON.Receive(ws, &gameparam)
|
||||||
log.Printf("gameparam: %+v", gameparam)
|
|
||||||
if gameparam.Type != "gameparam" {
|
if gameparam.Type != "gameparam" {
|
||||||
return nil, errors.New("didn't receive a good gameparam")
|
return nil, errors.New("didn't receive a good gameparam")
|
||||||
}
|
}
|
||||||
log.Printf("%+v", gameparam)
|
log.Printf("gameparam: %+v", gameparam)
|
||||||
|
|
||||||
conf := ClientConfig{
|
conf := ClientConfig{
|
||||||
ID: gameid,
|
ID: gameid,
|
||||||
@ -191,7 +111,7 @@ func negociate(ws *websocket.Conn, gameid string) (i *infos, err error) {
|
|||||||
if !handshake.Success {
|
if !handshake.Success {
|
||||||
return nil, errors.New("failed to validate correct stats request")
|
return nil, errors.New("failed to validate correct stats request")
|
||||||
}
|
}
|
||||||
log.Printf("%+v", handshake)
|
log.Printf("handshake: %+v", handshake)
|
||||||
return &infos{
|
return &infos{
|
||||||
id: idreq.AssignedID,
|
id: idreq.AssignedID,
|
||||||
width: gameparam.BoardSize.Width,
|
width: gameparam.BoardSize.Width,
|
||||||
@ -205,10 +125,7 @@ func main() {
|
|||||||
var gameid string
|
var gameid string
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if flag.NArg() < 1 {
|
if flag.NArg() < 1 {
|
||||||
gameid, err = createGame()
|
gameid = "debug"
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
gameid = flag.Arg(0)
|
gameid = flag.Arg(0)
|
||||||
}
|
}
|
||||||
@ -217,58 +134,55 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
board_info, err := negociate(ws, gameid)
|
boardInfo, err := negociate(ws, gameid)
|
||||||
|
log.Printf("boardInfo: %+v", boardInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: var target govector.Point2d
|
// TODO: var target govector.Point2d
|
||||||
moveto := govector.Point2d{
|
moveto := govector.Point2d{
|
||||||
X: rand.Float32() * board_info.width,
|
X: rand.Float32() * boardInfo.width,
|
||||||
Y: rand.Float32() * board_info.height,
|
Y: rand.Float32() * boardInfo.height,
|
||||||
}
|
}
|
||||||
log.Printf("%+v", moveto)
|
log.Printf("moveto: %+v", moveto)
|
||||||
|
|
||||||
var me Robot
|
var me Robot
|
||||||
|
|
||||||
log.Printf("negociated well, starting loop")
|
log.Printf("negociated well, starting loop")
|
||||||
for {
|
for {
|
||||||
var boardstate struct {
|
var boardstate Boardstate
|
||||||
Robots []Robot `json:"robots"`
|
|
||||||
Projectiles []Projectile `json:"projectiles"`
|
|
||||||
Splosions []Splosion `json:"splosions"`
|
|
||||||
Reset bool `json:"reset"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Turn int `json:"turn"`
|
|
||||||
}
|
|
||||||
err = websocket.JSON.Receive(ws, &boardstate)
|
err = websocket.JSON.Receive(ws, &boardstate)
|
||||||
|
// log.Printf("%#v", boardstate)
|
||||||
if *verbose {
|
if *verbose {
|
||||||
log.Printf("%+v", boardstate)
|
log.Printf("%+v", boardstate)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Connection lost")
|
log.Fatal("Connection lost")
|
||||||
}
|
}
|
||||||
for _, bot := range boardstate.Robots {
|
me = boardstate.MyRobots[0]
|
||||||
if bot.Id == board_info.id {
|
log.Printf("%+v", me)
|
||||||
me = bot
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if govector.Distance(me.Position, moveto) < 3.0 {
|
if govector.Distance(me.Position, moveto) < 3.0 {
|
||||||
log.Printf("old: %+v: %+v", me.Position, moveto)
|
log.Printf("old: %+v: %+v", me.Position, moveto)
|
||||||
moveto = govector.Point2d{
|
moveto = govector.Point2d{
|
||||||
X: rand.Float32() * board_info.width,
|
X: rand.Float32() * boardInfo.width,
|
||||||
Y: rand.Float32() * board_info.height,
|
Y: rand.Float32() * boardInfo.height,
|
||||||
}
|
}
|
||||||
log.Printf("new: %+v: %+v", me.Position, moveto)
|
log.Printf("new: %+v: %+v", me.Position, moveto)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: send instructions
|
// TODO: send instructions
|
||||||
err = websocket.JSON.Send(ws, Instruction{
|
instruction := map[string]Instruction{
|
||||||
MoveTo: &moveto,
|
me.Id: {
|
||||||
})
|
MoveTo: &moveto,
|
||||||
|
FireAt: &moveto,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
log.Printf("instruction: %+v", instruction)
|
||||||
|
err = websocket.JSON.Send(ws, instruction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
//log.Fatal("WIP")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user