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

View File

@ -14,8 +14,7 @@ import (
// 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.
type Player interface {
Recv(bs *server.Boardstate)
Instruction() map[string]server.Instruction
Update(bs *server.Boardstate) map[string]server.Instruction
}
// 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
func (p *SimplePlayer) Recv(bs *server.Boardstate) {
p.speed = p.maxSpeed
if len(bs.MyRobots) > 0 {
p.me = bs.MyRobots[0]
} else {
return
// Update is our implementation of recieving and processing a server.Boardstate
// from the server
func (p *SimplePlayer) Update(bs *server.Boardstate) map[string]server.Instruction {
instructions := make(map[string]server.Instruction)
for _, bot := range bs.MyRobots {
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,
}
}
return instructions
}
func (p *SimplePlayer) navigate() {
@ -81,12 +95,7 @@ func (p *SimplePlayer) navigate() {
}
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()
}
}
// XXX: need to keep track of seen objects ..
// simplest shooting strategy ... need to do the following:
// not shoot through buildings