Compare commits
2 Commits
master
...
robot-upgr
Author | SHA1 | Date | |
---|---|---|---|
|
f8ecde3e81 | ||
|
a2436fe8d8 |
10
client.go
10
client.go
@ -163,10 +163,14 @@ func (c *Client) Play() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprintf("%s: Connection likely lost: %s", c.Name, err))
|
return errors.New(fmt.Sprintf("%s: Connection likely lost: %s", c.Name, err))
|
||||||
}
|
}
|
||||||
c.Player.Recv(&c.boardstate)
|
|
||||||
|
|
||||||
instruction := c.Player.Instruction()
|
instructions := make(map[string]server.Instruction)
|
||||||
err = c.enc.Encode(instruction)
|
|
||||||
|
for _,bot := range(c.boardstate.MyRobots){
|
||||||
|
instructions[bot.Id] = c.Player.Update(&bot, &c.boardstate)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.enc.Encode(instructions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ var forceJSON = flag.Bool("json", false, "force json encoding")
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
var gameId string
|
var gameId string
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -39,6 +40,7 @@ func main() {
|
|||||||
gameId = flag.Arg(0)
|
gameId = flag.Arg(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.Verbose = *verbose
|
||||||
c := &client.Client{
|
c := &client.Client{
|
||||||
Server: *serverHostname,
|
Server: *serverHostname,
|
||||||
Port: *port,
|
Port: *port,
|
||||||
|
160
player.go
160
player.go
@ -9,92 +9,135 @@ import (
|
|||||||
"bitbucket.org/hackerbots/vector"
|
"bitbucket.org/hackerbots/vector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Verbose bool = false
|
||||||
|
|
||||||
// Player is the interface that is implemented when specifying non-default
|
// Player is the interface that is implemented when specifying non-default
|
||||||
// player behavior.
|
// player behavior.
|
||||||
//
|
//
|
||||||
// The general case will be to implement a Player type that contains the magic
|
// The general case will be to implement a Player type that contains the magic
|
||||||
// required to slay other robots quickly while staying alive for a long time.
|
// required to slay other robots quickly while staying alive for a long time.
|
||||||
type Player interface {
|
type Player interface {
|
||||||
Recv(bs *server.Boardstate)
|
Update(bot *server.Robot, bs *server.Boardstate) server.Instruction
|
||||||
Instruction() map[string]server.Instruction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SimplePlayer is our default player and stands as a starting point for your
|
// SimplePlayer is our default player and stands as a starting point for your
|
||||||
// own Player implementations.
|
// own Player implementations.
|
||||||
type SimplePlayer struct {
|
type SimplePlayer struct {
|
||||||
me server.Robot
|
me server.Robot
|
||||||
width, height float32
|
width, height float64
|
||||||
knownObstacles map[string]server.Obstacle
|
knownObstacles map[string]server.Obstacle
|
||||||
nearestEnemy *server.OtherRobot
|
nearestEnemy *server.OtherRobot
|
||||||
fireat *vector.Point2d
|
fireat *vector.Point2d
|
||||||
moveto *vector.Point2d
|
moveto *vector.Point2d
|
||||||
speed float32
|
speed float64
|
||||||
maxSpeed float32
|
maxSpeed float64
|
||||||
safeDistance float32
|
safeDistance float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSimplePlayer simply returns a populated, usable *SimplePlayer
|
// NewSimplePlayer simply returns a populated, usable *SimplePlayer
|
||||||
func NewSimplePlayer(width, height float32) *SimplePlayer {
|
func NewSimplePlayer(width, height float64) *SimplePlayer {
|
||||||
return &SimplePlayer{
|
return &SimplePlayer{
|
||||||
knownObstacles: make(map[string]server.Obstacle),
|
knownObstacles: make(map[string]server.Obstacle),
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
maxSpeed: 100,
|
maxSpeed: 1000,
|
||||||
safeDistance: 40,
|
safeDistance: 50,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recv is our implementation of receiving a server.Boardstate from the server
|
// Recv is our implementation of receiving a server.Boardstate from the server
|
||||||
func (p *SimplePlayer) Recv(bs *server.Boardstate) {
|
func (p *SimplePlayer) Update(bot *server.Robot, bs *server.Boardstate) server.Instruction{
|
||||||
p.speed = p.maxSpeed
|
p.me = *bot
|
||||||
if len(bs.MyRobots) > 0 {
|
p.speed = 1000
|
||||||
p.me = bs.MyRobots[0]
|
if p.me.Health <= 0{
|
||||||
} else {
|
return server.Instruction{}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.recon(bs)
|
p.recon(bs)
|
||||||
p.navigate()
|
p.navigate()
|
||||||
|
|
||||||
|
probe_point := p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))
|
||||||
|
|
||||||
|
if Verbose {
|
||||||
|
fmt.Printf("PROBE SENT: %v\n",probe_point)
|
||||||
|
}
|
||||||
|
|
||||||
|
return server.Instruction{
|
||||||
|
MoveTo: p.moveto,
|
||||||
|
TargetSpeed: &p.speed,
|
||||||
|
FireAt: p.fireat,
|
||||||
|
Probe: &probe_point,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SimplePlayer) navigate() {
|
func (p *SimplePlayer) navigate() {
|
||||||
|
if Verbose {
|
||||||
|
fmt.Printf("%v S:%v H:%v TS:%v\n\tX:%v Y:%v\n\tHX:%v HY:%v\n",
|
||||||
|
p.me.Name, p.me.Speed, p.me.Health, p.me.TargetSpeed,
|
||||||
|
p.me.Position.X, p.me.Position.Y,
|
||||||
|
p.me.Heading.X, p.me.Heading.Y)
|
||||||
|
|
||||||
|
if p.me.MoveTo != nil {
|
||||||
|
fmt.Printf("\tTX:%v TY:%v\n",
|
||||||
|
p.me.MoveTo.X, p.me.MoveTo.Y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if !p.probe(p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))) {
|
||||||
|
// if !p.probe(*p.moveto) {
|
||||||
|
// p.moveto = p.randomDirectionDrift(p.moveto, 20)
|
||||||
|
// // p.speed = p.maxSpeed
|
||||||
|
// fmt.Printf("Obstacle?\n")
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
if p.me.ProbeResult != nil {
|
||||||
|
p.moveto = p.randomDirectionDrift(&p.me.Position, 100)
|
||||||
|
p.speed = -20
|
||||||
|
if Verbose {
|
||||||
|
fmt.Printf("Probe %v\n", p.me.ProbeResult)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.me.Collision != nil {
|
||||||
|
p.moveto = p.randomDirectionDrift(&p.me.Position, 100)
|
||||||
|
p.speed = -20
|
||||||
|
if Verbose {
|
||||||
|
fmt.Printf("Hit!\n")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if p.moveto == nil {
|
if p.moveto == nil {
|
||||||
p.moveto = p.randomDirection()
|
p.moveto = p.randomDirection()
|
||||||
|
// p.speed = p.maxSpeed
|
||||||
|
if Verbose {
|
||||||
|
fmt.Printf("Start\n")
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
togo := p.me.Position.Sub(*p.moveto).Mag()
|
togo := p.me.Position.Sub(*p.moveto).Mag()
|
||||||
if togo < p.safeDistance+5 {
|
if togo < p.safeDistance+5 {
|
||||||
p.moveto = p.randomDirection()
|
p.moveto = p.randomDirection()
|
||||||
return
|
// p.speed = p.maxSpeed
|
||||||
}
|
if Verbose {
|
||||||
if !p.probe(p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))) {
|
fmt.Printf("New Dest\n")
|
||||||
p.speed = 0
|
|
||||||
if !p.probe(*p.moveto) {
|
|
||||||
p.moveto = p.randomDirection()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if p.me.Collision != nil {
|
|
||||||
p.moveto = p.randomDirection()
|
|
||||||
p.speed = 0
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SimplePlayer) recon(bs *server.Boardstate) {
|
func (p *SimplePlayer) recon(bs *server.Boardstate) {
|
||||||
for _, o := range bs.Objects {
|
|
||||||
obj := MiniObstacle(o)
|
|
||||||
if _, ok := p.knownObstacles[obj.Id()]; !ok {
|
|
||||||
p.knownObstacles[obj.Id()] = obj.ToObstacle()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// simplest shooting strategy ... need to do the following:
|
// simplest shooting strategy ... need to do the following:
|
||||||
// not shoot through buildings
|
// not shoot through buildings
|
||||||
// shoot at where the robot will be, not where it was.
|
// shoot at where the robot will be, not where it was.
|
||||||
p.nearestEnemy = nil
|
p.nearestEnemy = nil
|
||||||
p.fireat = nil
|
p.fireat = nil
|
||||||
closest := float32(math.Inf(1))
|
closest := math.Inf(1)
|
||||||
for _, enemy := range bs.OtherRobots {
|
for _, enemy := range bs.OtherRobots {
|
||||||
dist := p.me.Position.Sub(enemy.Position).Mag()
|
dist := p.me.Position.Sub(enemy.Position).Mag()
|
||||||
if dist < closest && dist > p.safeDistance {
|
if dist < closest && dist > p.safeDistance {
|
||||||
@ -107,22 +150,23 @@ func (p *SimplePlayer) recon(bs *server.Boardstate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instruction is our default implementation of preparing a map of information
|
func (p *SimplePlayer) randomDirectionDrift(start *vector.Point2d, drift float64) *vector.Point2d {
|
||||||
// to be sent to server.
|
for {
|
||||||
func (p *SimplePlayer) Instruction() map[string]server.Instruction {
|
pt := vector.Vector2d{
|
||||||
return map[string]server.Instruction{
|
X: start.X - drift + rand.Float64() * drift * 2,
|
||||||
p.me.Id: {
|
Y: start.Y - drift + rand.Float64() * drift * 2,
|
||||||
MoveTo: p.moveto,
|
}.ToPoint()
|
||||||
TargetSpeed: &p.speed,
|
|
||||||
FireAt: p.fireat,
|
if pt.X > 0 && pt.X < p.width && pt.Y > 0 && pt.Y < p.height {
|
||||||
},
|
return &pt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SimplePlayer) randomDirection() *vector.Point2d {
|
func (p *SimplePlayer) randomDirection() *vector.Point2d {
|
||||||
pt := vector.Vector2d{
|
pt := vector.Vector2d{
|
||||||
X: rand.Float32() * p.width,
|
X: rand.Float64() * p.width,
|
||||||
Y: rand.Float32() * p.height,
|
Y: rand.Float64() * p.height,
|
||||||
}.ToPoint()
|
}.ToPoint()
|
||||||
return &pt
|
return &pt
|
||||||
}
|
}
|
||||||
@ -141,31 +185,3 @@ func (p *SimplePlayer) probe(destination vector.Point2d) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// MiniObstacle is a convenient way to encode/decode between the [4]int -> server.Obstacle
|
|
||||||
type MiniObstacle [4]int
|
|
||||||
|
|
||||||
// id is used to calculate a key for use in maps
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToObstacle is where the conversion magic happens
|
|
||||||
func (mo *MiniObstacle) ToObstacle() server.Obstacle {
|
|
||||||
return server.Obstacle{
|
|
||||||
Bounds: vector.AABB2d{
|
|
||||||
A: vector.Point2d{X: float32(mo[0]), Y: float32(mo[1])},
|
|
||||||
B: vector.Point2d{X: float32(mo[2]), Y: float32(mo[3])},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user