bandwidth reduction, filtering explosions and sending objects as [4]int
This commit is contained in:
parent
5a2651d0dd
commit
58c084c7ab
34
game.go
34
game.go
@ -93,7 +93,7 @@ func NewGame(id string, width, height float32, obstacles, tick int) *game {
|
|||||||
|
|
||||||
func (g *game) tick(payload *Boardstate) {
|
func (g *game) tick(payload *Boardstate) {
|
||||||
g.players_remaining = 0
|
g.players_remaining = 0
|
||||||
payload.Obstacles = g.obstacles
|
payload.Objects = MinifyObstacles(g.obstacles)
|
||||||
|
|
||||||
// Update Players
|
// Update Players
|
||||||
for p := range g.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
|
// Copy the payload but only add the robots in scanner range
|
||||||
player_payload := NewBoardstate()
|
player_payload := NewBoardstate()
|
||||||
player_payload.Messages = payload.Messages
|
player_payload.Messages = payload.Messages
|
||||||
player_payload.Splosions = payload.Splosions
|
|
||||||
player_payload.Obstacles = payload.Obstacles
|
|
||||||
player_payload.AllBots = payload.AllBots
|
player_payload.AllBots = payload.AllBots
|
||||||
player_payload.Turn = payload.Turn
|
player_payload.Turn = payload.Turn
|
||||||
|
|
||||||
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 = append(
|
||||||
player_payload.OtherRobots,
|
// player_payload.OtherRobots,
|
||||||
r.GetTruncatedDetails())
|
// r.GetTruncatedDetails())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
player_payload.Objects = [][4]int{}
|
||||||
|
player_payload.Splosions = []Splosion{}
|
||||||
player_payload.Projectiles = []Projectile{}
|
player_payload.Projectiles = []Projectile{}
|
||||||
player_payload.Obstacles = []Obstacle{}
|
|
||||||
living_robots := 0
|
living_robots := 0
|
||||||
|
|
||||||
for _, r := range p.Robots {
|
for _, r := range p.Robots {
|
||||||
if r.Health > 0 {
|
if r.Health > 0 {
|
||||||
living_robots++
|
living_robots++
|
||||||
|
|
||||||
// Filter robots by scanner
|
// Filter robots by scanner
|
||||||
for player := range g.players {
|
for player := range g.players {
|
||||||
for _, scan_entry := range r.Scanners {
|
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
|
// Filter objects
|
||||||
for _, ob := range g.obstacles {
|
for _, ob := range g.obstacles {
|
||||||
if ob.distance_from_point(r.Position) < float32(r.Stats.ScannerRadius)+r.ScanCounter {
|
if ob.distance_from_point(r.Position) < float32(r.Stats.ScannerRadius)+r.ScanCounter {
|
||||||
player_payload.Obstacles = append(
|
player_payload.Objects = append(
|
||||||
player_payload.Obstacles,
|
player_payload.Objects, ob.minify())
|
||||||
ob)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +230,8 @@ func (g *game) sendUpdate(payload *Boardstate) {
|
|||||||
if living_robots == 0 {
|
if living_robots == 0 {
|
||||||
player_payload.OtherRobots = payload.OtherRobots
|
player_payload.OtherRobots = payload.OtherRobots
|
||||||
player_payload.Projectiles = payload.Projectiles
|
player_payload.Projectiles = payload.Projectiles
|
||||||
player_payload.Obstacles = payload.Obstacles
|
player_payload.Splosions = payload.Splosions
|
||||||
|
player_payload.Objects = payload.Objects
|
||||||
}
|
}
|
||||||
|
|
||||||
p.send <- player_payload
|
p.send <- player_payload
|
||||||
|
13
obstacle.go
13
obstacle.go
@ -21,6 +21,19 @@ func (o Obstacle) distance_from_point(p v.Point2d) float32 {
|
|||||||
return float32(dist)
|
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 {
|
func GenerateObstacles(count int, width, height float32) []Obstacle {
|
||||||
out := []Obstacle{}
|
out := []Obstacle{}
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
@ -110,6 +110,7 @@ type Boardstate struct {
|
|||||||
Projectiles []Projectile `json:"projectiles"`
|
Projectiles []Projectile `json:"projectiles"`
|
||||||
Splosions []Splosion `json:"splosions"`
|
Splosions []Splosion `json:"splosions"`
|
||||||
Obstacles []Obstacle `json:"obj"`
|
Obstacles []Obstacle `json:"obj"`
|
||||||
|
Objects [][4]int `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"`
|
||||||
|
13
robot.go
13
robot.go
@ -181,7 +181,7 @@ func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2
|
|||||||
if bot.Id == r.Id {
|
if bot.Id == r.Id {
|
||||||
continue
|
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)
|
collision, _, pos := v.RectIntersection(player_rect, r.Position, move_vector)
|
||||||
if collision {
|
if collision {
|
||||||
return collision, pos, bot
|
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 {
|
func (r *Robot) fire(projectiles map[*Projectile]bool, turn int) *Projectile {
|
||||||
|
Loading…
Reference in New Issue
Block a user