bandwidth reduction, filtering explosions and sending objects as [4]int

This commit is contained in:
Fraser Graham 2013-11-16 23:13:03 -08:00
parent 5a2651d0dd
commit 58c084c7ab
4 changed files with 49 additions and 12 deletions

34
game.go
View File

@ -93,7 +93,7 @@ func NewGame(id string, width, height float32, obstacles, tick int) *game {
func (g *game) tick(payload *Boardstate) {
g.players_remaining = 0
payload.Obstacles = g.obstacles
payload.Objects = MinifyObstacles(g.obstacles)
// Update Players
for p := range g.players {
@ -156,24 +156,25 @@ func (g *game) sendUpdate(payload *Boardstate) {
// Copy the payload but only add the robots in scanner range
player_payload := NewBoardstate()
player_payload.Messages = payload.Messages
player_payload.Splosions = payload.Splosions
player_payload.Obstacles = payload.Obstacles
player_payload.AllBots = payload.AllBots
player_payload.Turn = payload.Turn
for _, r := range p.Robots {
player_payload.MyRobots = append(player_payload.MyRobots, *r)
player_payload.OtherRobots = append(
player_payload.OtherRobots,
r.GetTruncatedDetails())
// player_payload.OtherRobots = append(
// player_payload.OtherRobots,
// r.GetTruncatedDetails())
}
player_payload.Objects = [][4]int{}
player_payload.Splosions = []Splosion{}
player_payload.Projectiles = []Projectile{}
player_payload.Obstacles = []Obstacle{}
living_robots := 0
for _, r := range p.Robots {
if r.Health > 0 {
living_robots++
// Filter robots by scanner
for player := range g.players {
for _, scan_entry := range r.Scanners {
@ -205,12 +206,22 @@ func (g *game) sendUpdate(payload *Boardstate) {
}
}
// Filter splosions
for splo := range g.splosions {
for _, scan_entry := range r.Scanners {
if splo.Id == scan_entry.Id {
player_payload.Splosions = append(
player_payload.Splosions,
*splo)
}
}
}
// Filter objects
for _, ob := range g.obstacles {
if ob.distance_from_point(r.Position) < float32(r.Stats.ScannerRadius)+r.ScanCounter {
player_payload.Obstacles = append(
player_payload.Obstacles,
ob)
player_payload.Objects = append(
player_payload.Objects, ob.minify())
}
}
}
@ -219,7 +230,8 @@ func (g *game) sendUpdate(payload *Boardstate) {
if living_robots == 0 {
player_payload.OtherRobots = payload.OtherRobots
player_payload.Projectiles = payload.Projectiles
player_payload.Obstacles = payload.Obstacles
player_payload.Splosions = payload.Splosions
player_payload.Objects = payload.Objects
}
p.send <- player_payload

View File

@ -21,6 +21,19 @@ func (o Obstacle) distance_from_point(p v.Point2d) float32 {
return float32(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
}
func MinifyObstacles(o []Obstacle) [][4]int {
out := [][4]int{}
for i := range o {
out = append(out, o[i].minify())
}
return out
}
func GenerateObstacles(count int, width, height float32) []Obstacle {
out := []Obstacle{}
for i := 0; i < count; i++ {

View File

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

View File

@ -181,7 +181,7 @@ func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2
if bot.Id == r.Id {
continue
}
player_rect := v.RectFromPoint(bot.Position, 3)
player_rect := v.RectFromPoint(bot.Position, 5)
collision, _, pos := v.RectIntersection(player_rect, r.Position, move_vector)
if collision {
return collision, pos, bot
@ -352,6 +352,17 @@ func (r *Robot) scan(g *game) {
}
}
for splo := range g.splosions {
dist := v.Distance(splo.Position, r.Position)
if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
s := Scanner{
Id: splo.Id,
Type: "explosion",
}
r.Scanners = append(r.Scanners, s)
}
}
}
func (r *Robot) fire(projectiles map[*Projectile]bool, turn int) *Projectile {