From f9b18eb2531501167903a7f7fbb5963411b98d72 Mon Sep 17 00:00:00 2001 From: Fraser Graham Date: Sat, 19 Oct 2013 00:00:45 -0700 Subject: [PATCH] Convert to 32-bit to save space in the serialization --- game.go | 10 +++++----- geom.go | 2 +- main.go | 4 ++-- player.go | 18 +++++++++--------- protocol.go | 6 +++--- robot.go | 53 +++++++++++++++++++++++++++-------------------------- 6 files changed, 47 insertions(+), 46 deletions(-) diff --git a/game.go b/game.go index 903725d..9b88df3 100644 --- a/game.go +++ b/game.go @@ -64,7 +64,7 @@ func (ml *MapLock) get(id string, force bool) (*game, error) { id = idg.Hash() } - _g := NewGame(id, *width, *height) + _g := NewGame(id, float32(*width), float32(*height)) go _g.run() ml.Lock() @@ -82,14 +82,14 @@ type game struct { register chan *player unregister chan *player turn int - width, height float64 + width, height float32 spectators map[*Spectator]bool sregister chan *Spectator sunregister chan *Spectator kill chan bool } -func NewGame(id string, width, height float64) *game { +func NewGame(id string, width, height float32) *game { g := &game{ id: id, register: make(chan *player), @@ -217,7 +217,7 @@ func (g *game) run() { func (g *game) nudgeProjectiles() (rprojectiles []Projectile) { rprojectiles = make([]Projectile, 0) for p := range g.projectiles { - newPos := move(p.Position, p.MoveTo, float64(p.Speed), delta) + newPos := move(p.Position, p.MoveTo, float32(p.Speed), delta) hit_player := false for player := range g.players { @@ -253,7 +253,7 @@ func (g *game) nudgeProjectiles() (rprojectiles []Projectile) { for player := range g.players { dist := v.Distance(player.Robot.Position, p.Position) - if dist < float64(p.Radius) { + if dist < float32(p.Radius) { // TODO map damage Max to Min based on distance from explosion if player.Robot.Health > 0 { diff --git a/geom.go b/geom.go index 90b6d80..0f4c8ae 100644 --- a/geom.go +++ b/geom.go @@ -4,7 +4,7 @@ import ( "bitbucket.org/hackerbots/vector" ) -func move(d1, d2 govector.Point2d, velocity float64, timeDelta float64) govector.Point2d { +func move(d1, d2 govector.Point2d, velocity float32, timeDelta float32) govector.Point2d { v := d2.Sub(d1) v_norm := v.Normalize() v_scaled := v_norm.Scale(velocity * timeDelta) diff --git a/main.go b/main.go index 1c4fbb7..df096d4 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ var height = flag.Float64("height", 550, "height of field") var profile = flag.String("pprof", "", "if specified will run with pprof") var debug = flag.Bool("debug", false, "automatically create games if they don't exist") -var delta float64 +var delta float32 var idg *IdGenerator @@ -40,7 +40,7 @@ func main() { games = MapLock{m: make(map[string]*game)} idg = NewIdGenerator() - delta = float64(*tick) / 1000 + delta = float32(*tick) / 1000 http.Handle("/ws/", websocket.Handler(addPlayer)) http.Handle("/game/start/", JsonHandler(startGame)) diff --git a/player.go b/player.go index bd58f6d..512d87e 100644 --- a/player.go +++ b/player.go @@ -110,12 +110,12 @@ func (p *player) nudge(g *game) { } // Max turn radius in this case is in degrees per second - if math.Abs(angle) > (float64(p.Robot.Stats.TurnSpeed) * delta) { + if float32(math.Abs(float64(angle))) > (float32(p.Robot.Stats.TurnSpeed) * delta) { // New heading should be a little less, take current heading and // rotate by the max turn radius per frame. - rot := (float64(p.Robot.Stats.TurnSpeed) * delta) * v.Deg2rad + rot := (float32(p.Robot.Stats.TurnSpeed) * delta) * v.Deg2rad - new_heading = current_heading.Rotate(rot * dir) + new_heading = current_heading.Rotate(rot * float32(dir)) } move_vector := new_heading.Scale(p.Robot.Speed * delta) @@ -146,7 +146,7 @@ func (p *player) scan(players map[*player]bool) { continue } dist := v.Distance(player.Robot.Position, p.Robot.Position) - if dist < float64(p.Robot.Stats.ScannerRadius) { + if dist < float32(p.Robot.Stats.ScannerRadius) { s := Scanner{ Position: v.Point2d{ X: player.Robot.Position.X, @@ -160,8 +160,8 @@ func (p *player) scan(players map[*player]bool) { func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile { // Throttle the fire rate - time_since_fired := (float64(turn) * (delta * 1000)) - (float64(p.Robot.LastFired) * (delta * 1000)) - if time_since_fired < float64(p.Robot.Stats.FireRate) { + time_since_fired := (float32(turn) * (delta * 1000)) - (float32(p.Robot.LastFired) * (delta * 1000)) + if time_since_fired < float32(p.Robot.Stats.FireRate) { return nil } @@ -173,14 +173,14 @@ func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile { MoveTo: *p.Robot.FireAt, Damage: 10, Radius: p.Robot.Stats.WeaponRadius, - Speed: float64(p.Robot.Stats.Speed * 3), + Speed: float32(p.Robot.Stats.Speed * 3), } } func (p *player) reset() { start_pos := v.Point2d{ - X: rand.Float64() * *width, - Y: rand.Float64() * *height, + X: rand.Float32() * float32(*width), + Y: rand.Float32() * float32(*height), } p.Robot.MoveTo = &start_pos p.Robot.Position = start_pos diff --git a/protocol.go b/protocol.go index c19254a..ba2f7ba 100644 --- a/protocol.go +++ b/protocol.go @@ -45,8 +45,8 @@ type ClientConfig struct { } type BoardSize struct { - Width float64 `json:"width"` - Height float64 `json:"height"` + Width float32 `json:"width"` + Height float32 `json:"height"` } type GameParam struct { @@ -55,7 +55,7 @@ type GameParam struct { } // > [OK | FULL | NOT AUTH], board size, game params -func NewGameParam(w, h float64) *GameParam { +func NewGameParam(w, h float32) *GameParam { return &GameParam{ BoardSize: BoardSize{ Width: w, diff --git a/robot.go b/robot.go index 173c89a..d0cc6d8 100644 --- a/robot.go +++ b/robot.go @@ -8,8 +8,8 @@ type Robot struct { Id string `json:"id"` Name string `json:"name"` Stats Stats `json:"stats"` - TargetSpeed float64 `json:"speed"` - Speed float64 `json:"speed"` + TargetSpeed float32 `json:"target_speed"` + Speed float32 `json:"speed"` Health int `json:"health"` Position v.Point2d `json:"position"` Heading v.Vector2d `json:"heading"` @@ -37,8 +37,8 @@ func (s RobotSorter) Less(i, j int) bool { type Stats struct { Hp int `json:"hp"` - Speed float64 `json:"speed"` - Acceleration float64 `json:"acceleration"` + Speed float32 `json:"speed"` + Acceleration float32 `json:"acceleration"` WeaponRadius int `json:"weapon_radius"` ScannerRadius int `json:"scanner_radius"` TurnSpeed int `json:"turn_speed"` @@ -61,33 +61,33 @@ func DeriveStats(request StatsRequest) Stats { s := Stats{} // Conversion Tables - hp_min := 20.0 - hp_max := 200.0 - s.Hp = int(((float64(request.Hp) / 100.0) * (hp_max - hp_min)) + hp_min) + var hp_min float32 = 20.0 + var hp_max float32 = 200.0 + s.Hp = int(((float32(request.Hp) / 100.0) * (hp_max - hp_min)) + hp_min) - speed_min := 40.0 - speed_max := 200.0 - s.Speed = ((float64(request.Speed) / 100.0) * (speed_max - speed_min)) + speed_min + var speed_min float32 = 40.0 + var speed_max float32 = 200.0 + s.Speed = ((float32(request.Speed) / 100.0) * (speed_max - speed_min)) + speed_min - accel_min := 20.0 - accel_max := 200.0 - s.Acceleration = ((float64(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min + var accel_min float32 = 20.0 + var accel_max float32 = 200.0 + s.Acceleration = ((float32(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min - wep_rad_min := 5.0 - wep_rad_max := 60.0 - s.WeaponRadius = int(((float64(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min) + var wep_rad_min float32 = 5.0 + var wep_rad_max float32 = 60.0 + s.WeaponRadius = int(((float32(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min) - scan_rad_min := 100.0 - scan_rad_max := 400.0 - s.ScannerRadius = int(((float64(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min) + var scan_rad_min float32 = 100.0 + var scan_rad_max float32 = 400.0 + s.ScannerRadius = int(((float32(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min) - turn_spd_min := 30.0 - turn_spd_max := 300.0 - s.TurnSpeed = int(((float64(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min) + var turn_spd_min float32 = 30.0 + var turn_spd_max float32 = 300.0 + s.TurnSpeed = int(((float32(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min) - fire_rate_min := 10.0 - fire_rate_max := 2000.0 - s.FireRate = int(fire_rate_max+300.0) - int(((float64(request.FireRate)/100.0)*(fire_rate_max-fire_rate_min))+fire_rate_min) + var fire_rate_min float32 = 10.0 + var fire_rate_max float32 = 2000.0 + s.FireRate = int(fire_rate_max+300.0) - int(((float32(request.FireRate)/100.0)*(fire_rate_max-fire_rate_min))+fire_rate_min) return s } @@ -96,6 +96,7 @@ func (s StatsRequest) Valid() bool { total := (s.Speed + s.Hp + s.WeaponRadius + s.ScannerRadius + s.Acceleration + s.TurnSpeed + s.FireRate) + // allowing for 50 pts in every category if total > 350 { return false } @@ -107,7 +108,7 @@ type Projectile struct { Position v.Point2d `json:"position"` MoveTo v.Point2d `json:"move_to"` Radius int `json:"radius"` - Speed float64 `json:"speed"` + Speed float32 `json:"speed"` Damage int `json:"damage"` }