simplest shooting algo

This commit is contained in:
Stephen McQuay 2014-01-11 00:08:08 -08:00
parent a1cc24f2a0
commit b16886cbac

View File

@ -6,11 +6,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math"
"math/rand" "math/rand"
"sync" "sync"
) )
const maxSpeed = 100 const maxSpeed = 100
const safeDistance = 20
func connect(server string, port int) (*websocket.Conn, error) { func connect(server string, port int) (*websocket.Conn, error) {
origin := "http://localhost/" origin := "http://localhost/"
@ -29,9 +31,11 @@ type robot struct {
speed float32 speed float32
moveto *govector.Point2d moveto *govector.Point2d
fireat *govector.Point2d
boardstate Boardstate boardstate Boardstate
me Robot me Robot
knownObstacles map[string]Obstacle knownObstacles map[string]Obstacle
nearestEnemy *OtherRobot
// 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
@ -143,7 +147,7 @@ func (r *robot) play() {
r.me.Id: { r.me.Id: {
MoveTo: r.moveto, MoveTo: r.moveto,
TargetSpeed: &r.speed, TargetSpeed: &r.speed,
// FireAt: moveto, FireAt: r.fireat,
}, },
} }
err = websocket.JSON.Send(r.ws, instruction) err = websocket.JSON.Send(r.ws, instruction)
@ -160,6 +164,23 @@ func (r *robot) recon() {
r.knownObstacles[o.id()] = o.toObstacle() r.knownObstacles[o.id()] = o.toObstacle()
} }
} }
// 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 {
point := r.nearestEnemy.Position.Add(r.nearestEnemy.Heading.Scale(20))
r.fireat = &point
}
} }
func (r *robot) selectDirection() *govector.Point2d { func (r *robot) selectDirection() *govector.Point2d {