Convert to 32-bit to save space in the serialization

This commit is contained in:
Fraser Graham 2013-10-19 00:00:45 -07:00
parent c074397c1f
commit f9b18eb253
6 changed files with 47 additions and 46 deletions

10
game.go
View File

@ -64,7 +64,7 @@ func (ml *MapLock) get(id string, force bool) (*game, error) {
id = idg.Hash() id = idg.Hash()
} }
_g := NewGame(id, *width, *height) _g := NewGame(id, float32(*width), float32(*height))
go _g.run() go _g.run()
ml.Lock() ml.Lock()
@ -82,14 +82,14 @@ type game struct {
register chan *player register chan *player
unregister chan *player unregister chan *player
turn int turn int
width, height float64 width, height float32
spectators map[*Spectator]bool spectators map[*Spectator]bool
sregister chan *Spectator sregister chan *Spectator
sunregister chan *Spectator sunregister chan *Spectator
kill chan bool kill chan bool
} }
func NewGame(id string, width, height float64) *game { func NewGame(id string, width, height float32) *game {
g := &game{ g := &game{
id: id, id: id,
register: make(chan *player), register: make(chan *player),
@ -217,7 +217,7 @@ func (g *game) run() {
func (g *game) nudgeProjectiles() (rprojectiles []Projectile) { func (g *game) nudgeProjectiles() (rprojectiles []Projectile) {
rprojectiles = make([]Projectile, 0) rprojectiles = make([]Projectile, 0)
for p := range g.projectiles { 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 hit_player := false
for player := range g.players { for player := range g.players {
@ -253,7 +253,7 @@ func (g *game) nudgeProjectiles() (rprojectiles []Projectile) {
for player := range g.players { for player := range g.players {
dist := v.Distance(player.Robot.Position, p.Position) 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 // TODO map damage Max to Min based on distance from explosion
if player.Robot.Health > 0 { if player.Robot.Health > 0 {

View File

@ -4,7 +4,7 @@ import (
"bitbucket.org/hackerbots/vector" "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 := d2.Sub(d1)
v_norm := v.Normalize() v_norm := v.Normalize()
v_scaled := v_norm.Scale(velocity * timeDelta) v_scaled := v_norm.Scale(velocity * timeDelta)

View File

@ -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 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 debug = flag.Bool("debug", false, "automatically create games if they don't exist")
var delta float64 var delta float32
var idg *IdGenerator var idg *IdGenerator
@ -40,7 +40,7 @@ func main() {
games = MapLock{m: make(map[string]*game)} games = MapLock{m: make(map[string]*game)}
idg = NewIdGenerator() idg = NewIdGenerator()
delta = float64(*tick) / 1000 delta = float32(*tick) / 1000
http.Handle("/ws/", websocket.Handler(addPlayer)) http.Handle("/ws/", websocket.Handler(addPlayer))
http.Handle("/game/start/", JsonHandler(startGame)) http.Handle("/game/start/", JsonHandler(startGame))

View File

@ -110,12 +110,12 @@ func (p *player) nudge(g *game) {
} }
// Max turn radius in this case is in degrees per second // 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 // New heading should be a little less, take current heading and
// rotate by the max turn radius per frame. // 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) move_vector := new_heading.Scale(p.Robot.Speed * delta)
@ -146,7 +146,7 @@ func (p *player) scan(players map[*player]bool) {
continue continue
} }
dist := v.Distance(player.Robot.Position, p.Robot.Position) 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{ s := Scanner{
Position: v.Point2d{ Position: v.Point2d{
X: player.Robot.Position.X, 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 { func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile {
// Throttle the fire rate // Throttle the fire rate
time_since_fired := (float64(turn) * (delta * 1000)) - (float64(p.Robot.LastFired) * (delta * 1000)) time_since_fired := (float32(turn) * (delta * 1000)) - (float32(p.Robot.LastFired) * (delta * 1000))
if time_since_fired < float64(p.Robot.Stats.FireRate) { if time_since_fired < float32(p.Robot.Stats.FireRate) {
return nil return nil
} }
@ -173,14 +173,14 @@ func (p *player) fire(projectiles map[*Projectile]bool, turn int) *Projectile {
MoveTo: *p.Robot.FireAt, MoveTo: *p.Robot.FireAt,
Damage: 10, Damage: 10,
Radius: p.Robot.Stats.WeaponRadius, Radius: p.Robot.Stats.WeaponRadius,
Speed: float64(p.Robot.Stats.Speed * 3), Speed: float32(p.Robot.Stats.Speed * 3),
} }
} }
func (p *player) reset() { func (p *player) reset() {
start_pos := v.Point2d{ start_pos := v.Point2d{
X: rand.Float64() * *width, X: rand.Float32() * float32(*width),
Y: rand.Float64() * *height, Y: rand.Float32() * float32(*height),
} }
p.Robot.MoveTo = &start_pos p.Robot.MoveTo = &start_pos
p.Robot.Position = start_pos p.Robot.Position = start_pos

View File

@ -45,8 +45,8 @@ type ClientConfig struct {
} }
type BoardSize struct { type BoardSize struct {
Width float64 `json:"width"` Width float32 `json:"width"`
Height float64 `json:"height"` Height float32 `json:"height"`
} }
type GameParam struct { type GameParam struct {
@ -55,7 +55,7 @@ type GameParam struct {
} }
// > [OK | FULL | NOT AUTH], board size, game params // > [OK | FULL | NOT AUTH], board size, game params
func NewGameParam(w, h float64) *GameParam { func NewGameParam(w, h float32) *GameParam {
return &GameParam{ return &GameParam{
BoardSize: BoardSize{ BoardSize: BoardSize{
Width: w, Width: w,

View File

@ -8,8 +8,8 @@ type Robot struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Stats Stats `json:"stats"` Stats Stats `json:"stats"`
TargetSpeed float64 `json:"speed"` TargetSpeed float32 `json:"target_speed"`
Speed float64 `json:"speed"` Speed float32 `json:"speed"`
Health int `json:"health"` Health int `json:"health"`
Position v.Point2d `json:"position"` Position v.Point2d `json:"position"`
Heading v.Vector2d `json:"heading"` Heading v.Vector2d `json:"heading"`
@ -37,8 +37,8 @@ func (s RobotSorter) Less(i, j int) bool {
type Stats struct { type Stats struct {
Hp int `json:"hp"` Hp int `json:"hp"`
Speed float64 `json:"speed"` Speed float32 `json:"speed"`
Acceleration float64 `json:"acceleration"` Acceleration float32 `json:"acceleration"`
WeaponRadius int `json:"weapon_radius"` WeaponRadius int `json:"weapon_radius"`
ScannerRadius int `json:"scanner_radius"` ScannerRadius int `json:"scanner_radius"`
TurnSpeed int `json:"turn_speed"` TurnSpeed int `json:"turn_speed"`
@ -61,33 +61,33 @@ func DeriveStats(request StatsRequest) Stats {
s := Stats{} s := Stats{}
// Conversion Tables // Conversion Tables
hp_min := 20.0 var hp_min float32 = 20.0
hp_max := 200.0 var hp_max float32 = 200.0
s.Hp = int(((float64(request.Hp) / 100.0) * (hp_max - hp_min)) + hp_min) s.Hp = int(((float32(request.Hp) / 100.0) * (hp_max - hp_min)) + hp_min)
speed_min := 40.0 var speed_min float32 = 40.0
speed_max := 200.0 var speed_max float32 = 200.0
s.Speed = ((float64(request.Speed) / 100.0) * (speed_max - speed_min)) + speed_min s.Speed = ((float32(request.Speed) / 100.0) * (speed_max - speed_min)) + speed_min
accel_min := 20.0 var accel_min float32 = 20.0
accel_max := 200.0 var accel_max float32 = 200.0
s.Acceleration = ((float64(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min s.Acceleration = ((float32(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min
wep_rad_min := 5.0 var wep_rad_min float32 = 5.0
wep_rad_max := 60.0 var wep_rad_max float32 = 60.0
s.WeaponRadius = int(((float64(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min) s.WeaponRadius = int(((float32(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min)
scan_rad_min := 100.0 var scan_rad_min float32 = 100.0
scan_rad_max := 400.0 var scan_rad_max float32 = 400.0
s.ScannerRadius = int(((float64(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min) s.ScannerRadius = int(((float32(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min)
turn_spd_min := 30.0 var turn_spd_min float32 = 30.0
turn_spd_max := 300.0 var turn_spd_max float32 = 300.0
s.TurnSpeed = int(((float64(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min) s.TurnSpeed = int(((float32(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min)
fire_rate_min := 10.0 var fire_rate_min float32 = 10.0
fire_rate_max := 2000.0 var fire_rate_max float32 = 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) 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 return s
} }
@ -96,6 +96,7 @@ func (s StatsRequest) Valid() bool {
total := (s.Speed + s.Hp + s.WeaponRadius + total := (s.Speed + s.Hp + s.WeaponRadius +
s.ScannerRadius + s.Acceleration + s.TurnSpeed + s.FireRate) s.ScannerRadius + s.Acceleration + s.TurnSpeed + s.FireRate)
// allowing for 50 pts in every category
if total > 350 { if total > 350 {
return false return false
} }
@ -107,7 +108,7 @@ type Projectile struct {
Position v.Point2d `json:"position"` Position v.Point2d `json:"position"`
MoveTo v.Point2d `json:"move_to"` MoveTo v.Point2d `json:"move_to"`
Radius int `json:"radius"` Radius int `json:"radius"`
Speed float64 `json:"speed"` Speed float32 `json:"speed"`
Damage int `json:"damage"` Damage int `json:"damage"`
} }