removed unecessary data from payload sent to the client and filter outgoing robot list by scanner contents for each player until they die

This commit is contained in:
Fraser Graham 2013-10-19 17:29:40 -07:00
parent f9b18eb253
commit fd13cb318a
3 changed files with 47 additions and 29 deletions

39
game.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
v "bitbucket.org/hackerbots/vector" v "bitbucket.org/hackerbots/vector"
// "encoding/json"
"errors" "errors"
"log" "log"
"sort" "sort"
@ -20,12 +21,6 @@ type Boardstate struct {
Turn int `json:"turn"` Turn int `json:"turn"`
} }
func (bs *Boardstate) EmptySlices() {
bs.Robots = bs.Robots[:0]
bs.Projectiles = bs.Projectiles[:0]
bs.Splosions = bs.Splosions[:0]
}
func NewBoardstate() *Boardstate { func NewBoardstate() *Boardstate {
return &Boardstate{ return &Boardstate{
Robots: []Robot{}, Robots: []Robot{},
@ -35,6 +30,7 @@ func NewBoardstate() *Boardstate {
} }
type Scanner struct { type Scanner struct {
Id string `json:"id"`
Position v.Point2d `json:"position"` Position v.Point2d `json:"position"`
Stats Stats `json:"stats"` Stats Stats `json:"stats"`
} }
@ -110,7 +106,6 @@ func NewGame(id string, width, height float32) *game {
func (g *game) run() { func (g *game) run() {
var t0, t1 time.Time var t0, t1 time.Time
payload := NewBoardstate()
ticker := time.NewTicker(time.Duration(*tick) * time.Millisecond) ticker := time.NewTicker(time.Duration(*tick) * time.Millisecond)
for { for {
select { select {
@ -134,9 +129,7 @@ func (g *game) run() {
delete(g.spectators, s) delete(g.spectators, s)
close(s.send) close(s.send)
case <-ticker.C: case <-ticker.C:
// XXX: This is very racy!! It was at bottom of loop and empty payload := NewBoardstate()
// stuff was going out
payload.EmptySlices()
g.turn++ g.turn++
payload.Turn = g.turn payload.Turn = g.turn
@ -152,6 +145,7 @@ func (g *game) run() {
robots_remaining := 0 robots_remaining := 0
// UPDATE GAME STATE
for p := range g.players { for p := range g.players {
if p.Robot.Health > 0 { if p.Robot.Health > 0 {
robots_remaining++ robots_remaining++
@ -199,8 +193,31 @@ func (g *game) run() {
log.Printf("Turn Processes %v\n", t1.Sub(t0)) log.Printf("Turn Processes %v\n", t1.Sub(t0))
} }
// SEND THE UPDATE TO EACH PLAYER
for p := range g.players { for p := range g.players {
p.send <- payload // Copy the payload but only add the robots in scanner range
player_payload := NewBoardstate()
player_payload.Projectiles = payload.Projectiles
player_payload.Splosions = payload.Splosions
player_payload.Turn = payload.Turn
player_payload.Reset = payload.Reset
player_payload.Robots = append(player_payload.Robots, p.Robot)
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)
}
}
}
} else {
player_payload.Robots = payload.Robots
}
// x, _ := json.Marshal(player_payload)
// log.Printf("%v", string(x))
p.send <- player_payload
} }
for s := range g.spectators { for s := range g.spectators {
s.send <- payload s.send <- payload

View File

@ -148,6 +148,7 @@ func (p *player) scan(players map[*player]bool) {
dist := v.Distance(player.Robot.Position, p.Robot.Position) dist := v.Distance(player.Robot.Position, p.Robot.Position)
if dist < float32(p.Robot.Stats.ScannerRadius) { if dist < float32(p.Robot.Stats.ScannerRadius) {
s := Scanner{ s := Scanner{
Id: player.Robot.Id,
Position: v.Point2d{ Position: v.Point2d{
X: player.Robot.Position.X, X: player.Robot.Position.X,
Y: player.Robot.Position.Y, Y: player.Robot.Position.Y,

View File

@ -7,16 +7,16 @@ import (
type Robot struct { type Robot struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Stats Stats `json:"stats"` Stats Stats `json:"-"`
TargetSpeed float32 `json:"target_speed"` TargetSpeed float32 `json:"-"`
Speed float32 `json:"speed"` Speed float32 `json:"speed"`
Health int `json:"health"` Health int `json:"health"`
Position v.Point2d `json:"position"` Position v.Point2d `json:"position"`
Heading v.Vector2d `json:"heading"` Heading v.Vector2d `json:"heading"`
MoveTo *v.Point2d `json:"move_to,omitempty"` MoveTo *v.Point2d `json:"-"`
FireAt *v.Point2d `json:"fire_at,omitempty"` FireAt *v.Point2d `json:"-"`
Scanners []Scanner `json:"scanners"` Scanners []Scanner `json:"scanners"`
LastFired int `json:"last_fired"` LastFired int `json:"-"`
} }
type RobotSorter struct { type RobotSorter struct {
@ -36,13 +36,13 @@ func (s RobotSorter) Less(i, j int) bool {
} }
type Stats struct { type Stats struct {
Hp int `json:"hp"` Hp int `json:"-"`
Speed float32 `json:"speed"` Speed float32 `json:"-"`
Acceleration float32 `json:"acceleration"` Acceleration float32 `json:"-"`
WeaponRadius int `json:"weapon_radius"` WeaponRadius int `json:"-"`
ScannerRadius int `json:"scanner_radius"` ScannerRadius int `json:"-"`
TurnSpeed int `json:"turn_speed"` TurnSpeed int `json:"-"`
FireRate int `json:"fire_rate"` FireRate int `json:"-"`
} }
// We request stats using an integer between 1 and 100, the // We request stats using an integer between 1 and 100, the
@ -106,19 +106,19 @@ func (s StatsRequest) Valid() bool {
type Projectile struct { type Projectile struct {
Id string `json:"id"` Id string `json:"id"`
Position v.Point2d `json:"position"` Position v.Point2d `json:"position"`
MoveTo v.Point2d `json:"move_to"` MoveTo v.Point2d `json:"-"`
Radius int `json:"radius"` Radius int `json:"radius"`
Speed float32 `json:"speed"` Speed float32 `json:"-"`
Damage int `json:"damage"` Damage int `json:"-"`
} }
type Splosion struct { type Splosion struct {
Id string `json:"id"` Id string `json:"id"`
Position v.Point2d `json:"position"` Position v.Point2d `json:"position"`
Radius int `json:"radius"` Radius int `json:"radius"`
MaxDamage int `json:"damage"` MaxDamage int `json:"-"`
MinDamage int `json:"damage"` MinDamage int `json:"-"`
Lifespan int `json:"lifespan"` Lifespan int `json:"-"`
} }
func (s *Splosion) Tick() { func (s *Splosion) Tick() {