reasonable object avoidance.

This commit is contained in:
Stephen McQuay 2014-01-10 01:22:33 -08:00
parent 82e54c28e7
commit a1cc24f2a0
3 changed files with 138 additions and 46 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"bitbucket.org/hackerbots/vector" "bitbucket.org/hackerbots/vector"
"fmt"
) )
type ClientConfig struct { type ClientConfig struct {
@ -101,16 +102,16 @@ type Instruction struct {
} }
type Boardstate struct { type Boardstate struct {
MyRobots []Robot `json:"my_robots"` MyRobots []Robot `json:"my_robots"`
OtherRobots []OtherRobot `json:"robots"` OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"` Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"` Splosions []Splosion `json:"splosions"`
Obstacles []Obstacle `json:"obj"` Obstacles []MiniObstacle `json:"objects"`
Reset bool `json:"reset"` Reset bool `json:"reset"`
Type string `json:"type"` Type string `json:"type"`
Turn int `json:"turn"` Turn int `json:"turn"`
AllBots []BotHealth `json:"all_bots"` AllBots []BotHealth `json:"all_bots"`
Messages []string `json:"messages"` Messages []string `json:"messages"`
} }
type OtherRobot struct { type OtherRobot struct {
@ -121,8 +122,34 @@ type OtherRobot struct {
Health int `json:"health"` Health int `json:"health"`
} }
type MiniObstacle [4]int
func (mo *MiniObstacle) id() string {
return fmt.Sprintf(
"%x%x%x%x",
mo[0],
mo[1],
mo[2],
mo[3],
)
}
func (mo MiniObstacle) String() string {
return mo.id()
}
func (mo *MiniObstacle) toObstacle() Obstacle {
return Obstacle{
Bounds: govector.AABB2d{
A: govector.Point2d{float32(mo[0]), float32(mo[1])},
B: govector.Point2d{float32(mo[2]), float32(mo[3])},
},
}
}
type Obstacle struct { type Obstacle struct {
Bounds govector.AABB2d `json:"bounds"` Bounds govector.AABB2d `json:"bounds"`
Hp int `json:"-"`
} }
type BotHealth struct { type BotHealth struct {

View File

@ -21,7 +21,7 @@ var weaponSpeed = flag.Int("wspeed", 50, "weapons speed")
// XXX: add TurnSpeed, WeaponDamage, WeaponSpeed // XXX: add TurnSpeed, WeaponDamage, WeaponSpeed
var server = flag.String("server", "hackerbots.us", "server hostname") var server = flag.String("server", "localhost", "server hostname")
var port = flag.Int("port", 8666, "server port") var port = flag.Int("port", 8666, "server port")
var botname = flag.String("name", "gobot", "the name that other players will see") var botname = flag.String("name", "gobot", "the name that other players will see")
@ -62,7 +62,8 @@ func main() {
WeaponDamage: *weaponDamage, WeaponDamage: *weaponDamage,
WeaponSpeed: *weaponSpeed, WeaponSpeed: *weaponSpeed,
}, },
wg: &wg, wg: &wg,
knownObstacles: make(map[string]Obstacle),
} }
wg.Add(1) wg.Add(1)
go r.play() go r.play()

132
robot.go
View File

@ -10,6 +10,8 @@ import (
"sync" "sync"
) )
const maxSpeed = 100
func connect(server string, port int) (*websocket.Conn, error) { func connect(server string, port int) (*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)
@ -25,6 +27,12 @@ type robot struct {
name string name string
stats StatsRequest stats StatsRequest
speed float32
moveto *govector.Point2d
boardstate Boardstate
me Robot
knownObstacles map[string]Obstacle
// TODO: don't know if I like how this is done ... I would rather send // TODO: don't know if I like how this is done ... I would rather send
// a signal over a chanel // a signal over a chanel
wg *sync.WaitGroup wg *sync.WaitGroup
@ -106,47 +114,103 @@ func (r *robot) play() {
return return
} }
log.Printf("%s: %+v", r.name, r)
// TODO: var target govector.Point2d
moveto := govector.Point2d{
X: rand.Float32() * r.game.BoardSize.Width,
Y: rand.Float32() * r.game.BoardSize.Height,
}
log.Printf("%s: moveto: %+v", r.name, moveto)
log.Printf("%s: starting loop", r.name) log.Printf("%s: starting loop", r.name)
for { for {
var boardstate Boardstate r.speed = float32(maxSpeed)
err = websocket.JSON.Receive(r.ws, &boardstate) err = websocket.JSON.Receive(r.ws, &r.boardstate)
if *verbose {
log.Printf("%+v", boardstate)
}
if err != nil { if err != nil {
log.Printf("%s: Connection lost", r.name) log.Printf("%s: Connection lost", r.name)
return return
} }
for _, me := range boardstate.MyRobots { if *verbose {
me = boardstate.MyRobots[0] log.Printf("\n\n%#v\n\n", r.boardstate)
if govector.Distance(me.Position, moveto) < 3.0 { }
log.Printf("%s: old: %+v: %+v", r.name, me.Position, moveto)
moveto = govector.Point2d{
X: rand.Float32() * r.game.BoardSize.Width,
Y: rand.Float32() * r.game.BoardSize.Height,
}
log.Printf("%s: new: %+v: %+v", r.name, me.Position, moveto)
}
// TODO: send instructions r.recon()
instruction := map[string]Instruction{
me.Id: { if len(r.boardstate.MyRobots) > 0 {
MoveTo: &moveto, r.me = r.boardstate.MyRobots[0]
FireAt: &moveto, } else {
}, // XXX
} log.Println("continue")
err = websocket.JSON.Send(r.ws, instruction) continue
if err != nil { }
log.Println(err)
return r.navigate()
}
instruction := map[string]Instruction{
r.me.Id: {
MoveTo: r.moveto,
TargetSpeed: &r.speed,
// FireAt: moveto,
},
}
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()
}
}
}
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()
} }
} }
} }