diff --git a/robot.go b/robot.go index a44e83d..9c69c3f 100644 --- a/robot.go +++ b/robot.go @@ -6,11 +6,13 @@ import ( "errors" "fmt" "log" + "math" "math/rand" "sync" ) const maxSpeed = 100 +const safeDistance = 20 func connect(server string, port int) (*websocket.Conn, error) { origin := "http://localhost/" @@ -29,9 +31,11 @@ type robot struct { speed float32 moveto *govector.Point2d + fireat *govector.Point2d boardstate Boardstate me Robot knownObstacles map[string]Obstacle + nearestEnemy *OtherRobot // TODO: don't know if I like how this is done ... I would rather send // a signal over a chanel @@ -143,7 +147,7 @@ func (r *robot) play() { r.me.Id: { MoveTo: r.moveto, TargetSpeed: &r.speed, - // FireAt: moveto, + FireAt: r.fireat, }, } err = websocket.JSON.Send(r.ws, instruction) @@ -160,6 +164,23 @@ func (r *robot) recon() { 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 {