Browse Source

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

master
Fraser Graham 7 years ago
parent
commit
fd13cb318a
3 changed files with 47 additions and 29 deletions
  1. +28
    -11
      game.go
  2. +1
    -0
      player.go
  3. +18
    -18
      robot.go

+ 28
- 11
game.go View File

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


+ 1
- 0
player.go View File

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


+ 18
- 18
robot.go View File

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


Loading…
Cancel
Save