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 (
"bitbucket.org/hackerbots/vector"
"fmt"
)
type ClientConfig struct {
@ -105,7 +106,7 @@ type Boardstate struct {
OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"`
Obstacles []Obstacle `json:"obj"`
Obstacles []MiniObstacle `json:"objects"`
Reset bool `json:"reset"`
Type string `json:"type"`
Turn int `json:"turn"`
@ -121,8 +122,34 @@ type OtherRobot struct {
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 {
Bounds govector.AABB2d `json:"bounds"`
Hp int `json:"-"`
}
type BotHealth struct {

View File

@ -21,7 +21,7 @@ var weaponSpeed = flag.Int("wspeed", 50, "weapons speed")
// 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 botname = flag.String("name", "gobot", "the name that other players will see")
@ -63,6 +63,7 @@ func main() {
WeaponSpeed: *weaponSpeed,
},
wg: &wg,
knownObstacles: make(map[string]Obstacle),
}
wg.Add(1)
go r.play()

114
robot.go
View File

@ -10,6 +10,8 @@ import (
"sync"
)
const maxSpeed = 100
func connect(server string, port int) (*websocket.Conn, error) {
origin := "http://localhost/"
url := fmt.Sprintf("ws://%s:%d/ws/", server, port)
@ -25,6 +27,12 @@ type robot struct {
name string
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
// a signal over a chanel
wg *sync.WaitGroup
@ -106,40 +114,36 @@ func (r *robot) play() {
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)
for {
var boardstate Boardstate
err = websocket.JSON.Receive(r.ws, &boardstate)
if *verbose {
log.Printf("%+v", boardstate)
}
r.speed = float32(maxSpeed)
err = websocket.JSON.Receive(r.ws, &r.boardstate)
if err != nil {
log.Printf("%s: Connection lost", r.name)
return
}
for _, me := range boardstate.MyRobots {
me = boardstate.MyRobots[0]
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)
if *verbose {
log.Printf("\n\n%#v\n\n", r.boardstate)
}
// TODO: send instructions
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{
me.Id: {
MoveTo: &moveto,
FireAt: &moveto,
r.me.Id: {
MoveTo: r.moveto,
TargetSpeed: &r.speed,
// FireAt: moveto,
},
}
err = websocket.JSON.Send(r.ws, instruction)
@ -148,5 +152,65 @@ func (r *robot) play() {
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()
}
}
}