From de68ec2e7b25d053788520ba771765ba47c986f0 Mon Sep 17 00:00:00 2001 From: Fraser Graham Date: Thu, 24 Oct 2013 20:13:21 -0700 Subject: [PATCH] Create a stripped down version of the robot struct to represent all robots and break up the update into my_robots and robots. Has dependent change on client --- game.go | 25 +++++++++++++++++-------- player.go | 1 - robot.go | 20 +++++++++++++++++++- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/game.go b/game.go index a9f8876..d6d43fd 100644 --- a/game.go +++ b/game.go @@ -18,7 +18,8 @@ type BotHealth struct { } type Boardstate struct { - Robots []Robot `json:"robots"` + MyRobots []Robot `json:"my_robots"` + OtherRobots []OtherRobot `json:"robots"` Projectiles []Projectile `json:"projectiles"` Splosions []Splosion `json:"splosions"` Reset bool `json:"reset"` @@ -29,7 +30,8 @@ type Boardstate struct { func NewBoardstate() *Boardstate { return &Boardstate{ - Robots: []Robot{}, + MyRobots: []Robot{}, + OtherRobots: []OtherRobot{}, Projectiles: []Projectile{}, Splosions: []Splosion{}, AllBots: []BotHealth{}, @@ -40,7 +42,7 @@ func NewBoardstate() *Boardstate { type Scanner struct { Id string `json:"id"` Position v.Point2d `json:"position"` - Stats Stats `json:"stats"` + Type string `json:"type"` } type MapLock struct { @@ -121,7 +123,9 @@ func (g *game) tick(payload *Boardstate) int { robots_remaining++ p.Tick(g) } - payload.Robots = append(payload.Robots, p.Robot) + payload.OtherRobots = append( + payload.OtherRobots, + p.Robot.GetTruncatedDetails()) payload.AllBots = append( payload.AllBots, @@ -152,7 +156,7 @@ func (g *game) tick(payload *Boardstate) int { func (g *game) send_update(payload *Boardstate) { // Ensure that the robots are always sent in a consistent order - sort.Sort(RobotSorter{Robots: payload.Robots}) + sort.Sort(RobotSorter{Robots: payload.OtherRobots}) for p := range g.players { // Copy the payload but only add the robots in scanner range @@ -162,18 +166,23 @@ func (g *game) send_update(payload *Boardstate) { player_payload.AllBots = payload.AllBots player_payload.Turn = payload.Turn player_payload.Reset = payload.Reset - player_payload.Robots = append(player_payload.Robots, p.Robot) + player_payload.MyRobots = append(player_payload.MyRobots, p.Robot) + player_payload.OtherRobots = append( + player_payload.OtherRobots, + p.Robot.GetTruncatedDetails()) if p.Robot.Health > 0 { for player := range g.players { for _, scan_entry := range p.Robot.Scanners { if player.Robot.Id == scan_entry.Id { - player_payload.Robots = append(player_payload.Robots, player.Robot) + player_payload.OtherRobots = append( + player_payload.OtherRobots, + player.Robot.GetTruncatedDetails()) } } } } else { - player_payload.Robots = payload.Robots + player_payload.OtherRobots = payload.OtherRobots } // x, _ := json.Marshal(player_payload) diff --git a/player.go b/player.go index 1d79968..d0392df 100644 --- a/player.go +++ b/player.go @@ -71,7 +71,6 @@ func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point } // Check Other Bots - // TODO - FIX THIS for player := range g.players { if player.Robot.Id == p.Robot.Id { continue diff --git a/robot.go b/robot.go index 470fcc1..6a94f73 100644 --- a/robot.go +++ b/robot.go @@ -19,8 +19,26 @@ type Robot struct { LastFired int `json:"-"` } +// This is the subset of data we send to players about robots +// that are not theirs. +type OtherRobot struct { + Id string `json:"id"` + Name string `json:"name"` + Position v.Point2d `json:"position"` + Health int `json:"health"` +} + +func (r Robot) GetTruncatedDetails() OtherRobot { + return OtherRobot{ + Id: r.Id, + Name: r.Name, + Position: r.Position, + Health: r.Health, + } +} + type RobotSorter struct { - Robots []Robot + Robots []OtherRobot } func (s RobotSorter) Len() int {