Make obstacles know how to minify themselves

including a fix for using pointers instead of values.
This commit is contained in:
Stephen McQuay 2014-04-26 15:02:37 -07:00
parent 8f7ef5aa1a
commit 961cf5d7da
3 changed files with 30 additions and 30 deletions

18
game.go
View File

@ -135,7 +135,6 @@ func NewGame(id string, width, height float64, obstacles, tick, maxPoints int, m
// tick is the method called every TICK ms. // tick is the method called every TICK ms.
func (g *Game) tick(payload *Boardstate) { func (g *Game) tick(payload *Boardstate) {
g.players_remaining = 0 g.players_remaining = 0
payload.Objects = MinifyObstacles(g.obstacles)
// Update Players // Update Players
for p := range g.players { for p := range g.players {
@ -205,12 +204,9 @@ func (g *Game) sendUpdate(payload *Boardstate) {
for _, r := range p.Robots { for _, r := range p.Robots {
player_payload.MyRobots = append(player_payload.MyRobots, *r) player_payload.MyRobots = append(player_payload.MyRobots, *r)
// player_payload.OtherRobots = append(
// player_payload.OtherRobots,
// r.GetTruncatedDetails())
} }
player_payload.Objects = [][4]int{} player_payload.Obstacles = []Obstacle{}
player_payload.Splosions = []Splosion{} player_payload.Splosions = []Splosion{}
player_payload.Projectiles = []Projectile{} player_payload.Projectiles = []Projectile{}
living_robots := 0 living_robots := 0
@ -264,23 +260,17 @@ func (g *Game) sendUpdate(payload *Boardstate) {
// Filter objects // Filter objects
for _, ob := range g.obstacles { for _, ob := range g.obstacles {
if ob.distance_from_point(r.Position) < float64(r.Stats.ScannerRadius)+r.ScanCounter { if ob.distance_from_point(r.Position) < float64(r.Stats.ScannerRadius)+r.ScanCounter {
player_payload.Objects = append( player_payload.Obstacles = append(
player_payload.Objects, ob.minify()) player_payload.Obstacles, ob)
} }
} }
} }
} }
// if living_robots == 0 {
// player_payload.OtherRobots = payload.OtherRobots
// player_payload.Projectiles = payload.Projectiles
// player_payload.Splosions = payload.Splosions
// player_payload.Objects = payload.Objects
// }
p.send <- player_payload p.send <- player_payload
} }
for s := range g.spectators { for s := range g.spectators {
payload.Obstacles = g.obstacles
s.send <- payload s.send <- payload
} }

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"encoding/json"
"math" "math"
"math/rand" "math/rand"
@ -13,6 +14,30 @@ type Obstacle struct {
Hp int `json:"-"` Hp int `json:"-"`
} }
func (o *Obstacle) MarshalJSON() ([]byte, error) {
payload := [4]float64{
o.Bounds.A.X,
o.Bounds.A.Y,
o.Bounds.B.X,
o.Bounds.B.Y,
}
return json.Marshal(payload)
}
func (o *Obstacle) UnmarshalJSON(incoming []byte) error {
payload := [4]float64{}
err := json.Unmarshal(incoming, &payload)
if err != nil {
return err
}
o.Bounds = v.AABB2d{
A: v.Point2d{X: payload[0], Y: payload[1]},
B: v.Point2d{X: payload[2], Y: payload[3]},
}
return nil
}
func (o Obstacle) distance_from_point(p v.Point2d) float64 { func (o Obstacle) distance_from_point(p v.Point2d) float64 {
dist := math.MaxFloat32 dist := math.MaxFloat32
@ -24,21 +49,6 @@ func (o Obstacle) distance_from_point(p v.Point2d) float64 {
return dist return dist
} }
func (o Obstacle) minify() [4]int {
out := [4]int{int(o.Bounds.A.X), int(o.Bounds.A.Y), int(o.Bounds.B.X), int(o.Bounds.B.Y)}
return out
}
// MinifyObstacles is a function used to convert []osbstacles into more tightly
// packed [][4]int for smaller json payloads
func MinifyObstacles(o []Obstacle) [][4]int {
out := [][4]int{}
for i := range o {
out = append(out, o[i].minify())
}
return out
}
// GenerateObstacles returns a slice of (count) obstacles within a region // GenerateObstacles returns a slice of (count) obstacles within a region
// bounded by width, height. // bounded by width, height.
func GenerateObstacles(count int, width, height float64) []Obstacle { func GenerateObstacles(count int, width, height float64) []Obstacle {

View File

@ -115,7 +115,7 @@ type Boardstate struct {
OtherRobots []OtherRobot `json:"robots"` OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"` Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"` Splosions []Splosion `json:"splosions"`
Objects [][4]int `json:"objects"` Obstacles []Obstacle `json:"objects"`
Type string `json:"type"` Type string `json:"type"`
Turn int `json:"turn"` Turn int `json:"turn"`
AllBots []BotHealth `json:"all_bots"` AllBots []BotHealth `json:"all_bots"`