Convert to 32-bit to save space in the serialization

This commit is contained in:
Fraser Graham
2013-10-19 00:00:51 -07:00
parent c074397c1f
commit f9b18eb253
6 changed files with 47 additions and 46 deletions
+27 -26
View File
@@ -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"`
}