Update to new protocol (Update)

This commit is contained in:
Stephen McQuay 2014-05-11 23:13:00 -07:00
parent d3e7a247c8
commit 14297191ed
2 changed files with 30 additions and 23 deletions

View File

@ -31,7 +31,7 @@ type Client struct {
Player Player Player Player
Game server.GameParam Game server.GameParam
boardstate server.Boardstate boardstate *server.Boardstate
enc encoder enc encoder
dec decoder dec decoder
ws *websocket.Conn ws *websocket.Conn
@ -164,16 +164,14 @@ func (c *Client) Negociate(clientType string) (err error) {
func (c *Client) Play() error { func (c *Client) Play() error {
log.Printf("%s: starting loop", c.Name) log.Printf("%s: starting loop", c.Name)
bs := &server.Boardstate{}
var err error var err error
for { for {
err = c.dec.Decode(&c.boardstate) err = c.dec.Decode(bs)
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) err = c.enc.Encode(c.Player.Update(bs))
instruction := c.Player.Instruction()
err = c.enc.Encode(instruction)
if err != nil { if err != nil {
return err return err
} }

View File

@ -14,8 +14,7 @@ import (
// 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(bs *server.Boardstate) map[string]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
@ -43,17 +42,32 @@ func NewSimplePlayer(width, height float64) *SimplePlayer {
} }
} }
// Recv is our implementation of receiving a server.Boardstate from the server // Update is our implementation of recieving and processing a server.Boardstate
func (p *SimplePlayer) Recv(bs *server.Boardstate) { // from the server
p.speed = p.maxSpeed func (p *SimplePlayer) Update(bs *server.Boardstate) map[string]server.Instruction {
if len(bs.MyRobots) > 0 { instructions := make(map[string]server.Instruction)
p.me = bs.MyRobots[0]
} else { for _, bot := range bs.MyRobots {
return p.me = bot
p.speed = 1000
if p.me.Health <= 0 {
continue
}
p.recon(bs)
p.navigate()
probe_point := p.me.Position.Add(p.me.Heading.Scale(p.safeDistance))
instructions[bot.Id] = server.Instruction{
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
Probe: &probe_point,
}
} }
p.recon(bs) return instructions
p.navigate()
} }
func (p *SimplePlayer) navigate() { func (p *SimplePlayer) navigate() {
@ -81,12 +95,7 @@ func (p *SimplePlayer) navigate() {
} }
func (p *SimplePlayer) recon(bs *server.Boardstate) { func (p *SimplePlayer) recon(bs *server.Boardstate) {
for _, o := range bs.Objects { // XXX: need to keep track of seen 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