Browse Source

Make obstacles know how to minify themselves

including a fix for using pointers instead of values.
master
Stephen McQuay 5 years ago
parent
commit
961cf5d7da
3 changed files with 30 additions and 30 deletions
  1. +4
    -14
      game.go
  2. +25
    -15
      obstacle.go
  3. +1
    -1
      protocol.go

+ 4
- 14
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.
func (g *Game) tick(payload *Boardstate) {
g.players_remaining = 0
payload.Objects = MinifyObstacles(g.obstacles)

// Update Players
for p := range g.players {
@@ -205,12 +204,9 @@ func (g *Game) sendUpdate(payload *Boardstate) {

for _, r := range p.Robots {
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.Projectiles = []Projectile{}
living_robots := 0
@@ -264,23 +260,17 @@ func (g *Game) sendUpdate(payload *Boardstate) {
// Filter objects
for _, ob := range g.obstacles {
if ob.distance_from_point(r.Position) < float64(r.Stats.ScannerRadius)+r.ScanCounter {
player_payload.Objects = append(
player_payload.Objects, ob.minify())
player_payload.Obstacles = append(
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
}
for s := range g.spectators {
payload.Obstacles = g.obstacles
s.send <- payload
}


+ 25
- 15
obstacle.go View File

@@ -1,6 +1,7 @@
package server

import (
"encoding/json"
"math"
"math/rand"

@@ -13,6 +14,30 @@ type Obstacle struct {
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 {
dist := math.MaxFloat32

@@ -24,21 +49,6 @@ func (o Obstacle) distance_from_point(p v.Point2d) float64 {
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
// bounded by width, height.
func GenerateObstacles(count int, width, height float64) []Obstacle {

+ 1
- 1
protocol.go View File

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

Loading…
Cancel
Save