From 8f7ef5aa1afca6c54081d2d4fca0252fcb9b48e6 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sat, 26 Apr 2014 14:56:12 -0700 Subject: [PATCH] float32 -> float64 --- config.go | 21 ++++++----- control.go | 6 +-- game.go | 8 ++-- obstacle.go | 14 +++---- player.go | 2 +- projectile.go | 6 +-- protocol.go | 8 ++-- robot.go | 100 +++++++++++++++++++++++++------------------------- 8 files changed, 83 insertions(+), 82 deletions(-) diff --git a/config.go b/config.go index 659a957..6f787dd 100644 --- a/config.go +++ b/config.go @@ -15,14 +15,15 @@ import ( // ways (json POSTed to server, config file, constants) and are typically owned // by a Controller. type Config struct { - Tick int `json:"tick"` // ms - Timescale float32 `json:"timescale"` - Delta float32 `json:"delta"` - Width int `json:"width"` - Height int `json:"height"` - ObstacleCount int `json:"obstacle_count"` - MaxPoints int `json:"max_points"` - Mode string `json:"mode"` + Tick int `json:"tick"` // ms + Timescale float64 `json:"timescale"` + Delta float64 `json:"delta"` + Width int `json:"width"` + Height int `json:"height"` + ObstacleCount int `json:"obstacle_count"` + Obstacles []Obstacle `json:'obstacles'` + MaxPoints int `json:"max_points"` + Mode string `json:"mode"` } const ( @@ -69,7 +70,7 @@ func LoadConfig(filename string) (Config, error) { return c, errors.New(fmt.Sprintf("config parse error: %s", err)) } } - c.Delta = (float32(c.Tick) / 1000.0) * float32(c.Timescale) - log.Printf("final config: %+v", c) + c.Delta = float64(c.Tick) / 1000.0 * c.Timescale + log.Printf("final config: %#v", c) return c, nil } diff --git a/control.go b/control.go index 3593446..cab3452 100644 --- a/control.go +++ b/control.go @@ -62,7 +62,7 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { log.Println("asked to create a game") requested_game_name := c.Idg.Hash() - width, height := float32(c.Conf.Width), float32(c.Conf.Height) + width, height := float64(c.Conf.Width), float64(c.Conf.Height) obstacleCount := 0 maxPoints := c.Conf.MaxPoints mode := "deathmatch" @@ -87,8 +87,8 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { return } requested_game_name = cfg.Name - width = float32(cfg.Width) - height = float32(cfg.Height) + width = float64(cfg.Width) + height = float64(cfg.Height) obstacleCount = cfg.ObstacleCount maxPoints = cfg.MaxPoints mode = cfg.Mode diff --git a/game.go b/game.go index decb270..81fed92 100644 --- a/game.go +++ b/game.go @@ -65,14 +65,14 @@ type Game struct { unregister chan *Player turn int players_remaining int - width, height float32 + width, height float64 maxPoints int spectators map[*Spectator]bool sregister chan *Spectator sunregister chan *Spectator kill chan bool repair_hp int - repair_rate float32 + repair_rate float64 tick_duration int stats GameStats mode GameMode @@ -87,7 +87,7 @@ type GameMode interface { } // NewGame Poplulates a Game struct and starts the bandwidth calculator. -func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) (*Game, error) { +func NewGame(id string, width, height float64, obstacles, tick, maxPoints int, mode string) (*Game, error) { bw, err := bandwidth.NewBandwidth( []int{1, 10, 60}, 1*time.Second, @@ -263,7 +263,7 @@ func (g *Game) sendUpdate(payload *Boardstate) { // Filter objects 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) < float64(r.Stats.ScannerRadius)+r.ScanCounter { player_payload.Objects = append( player_payload.Objects, ob.minify()) } diff --git a/obstacle.go b/obstacle.go index 150fb6f..b9708d5 100644 --- a/obstacle.go +++ b/obstacle.go @@ -13,7 +13,7 @@ type Obstacle struct { Hp int `json:"-"` } -func (o Obstacle) distance_from_point(p v.Point2d) float32 { +func (o Obstacle) distance_from_point(p v.Point2d) float64 { dist := math.MaxFloat32 dist = math.Min(dist, float64(p.Sub(o.Bounds.A).Mag())) @@ -21,7 +21,7 @@ func (o Obstacle) distance_from_point(p v.Point2d) float32 { dist = math.Min(dist, float64(p.Sub(v.Point2d{X: o.Bounds.A.X, Y: o.Bounds.B.Y}).Mag())) dist = math.Min(dist, float64(p.Sub(v.Point2d{X: o.Bounds.B.X, Y: o.Bounds.A.Y}).Mag())) - return float32(dist) + return dist } func (o Obstacle) minify() [4]int { @@ -41,13 +41,13 @@ func MinifyObstacles(o []Obstacle) [][4]int { // GenerateObstacles returns a slice of (count) obstacles within a region // bounded by width, height. -func GenerateObstacles(count int, width, height float32) []Obstacle { +func GenerateObstacles(count int, width, height float64) []Obstacle { out := []Obstacle{} for i := 0; i < count; i++ { - x := rand.Float32() * (width - (width / 10)) - y := rand.Float32() * (height - (height / 10)) - w := rand.Float32() * (width / 10) - h := rand.Float32() * (height / 10) + x := rand.Float64() * (width - (width / 10)) + y := rand.Float64() * (height - (height / 10)) + w := rand.Float64() * (width / 10) + h := rand.Float64() * (height / 10) out = append( out, Obstacle{ diff --git a/player.go b/player.go index 308e321..853ff0d 100644 --- a/player.go +++ b/player.go @@ -159,7 +159,7 @@ func (p *Player) Recv() { } if msg.TargetSpeed != nil { - r.TargetSpeed = float32(*msg.TargetSpeed) + r.TargetSpeed = *msg.TargetSpeed } else { r.TargetSpeed = r.Stats.Speed } diff --git a/projectile.go b/projectile.go index c288d34..f7eef5e 100644 --- a/projectile.go +++ b/projectile.go @@ -12,10 +12,10 @@ type Projectile struct { Position v.Point2d `json:"position"` MoveTo v.Point2d `json:"-"` Radius int `json:"-"` - Speed float32 `json:"-"` + Speed float64 `json:"-"` Damage int `json:"-"` Owner *Robot `json:"-"` - Delta float32 + Delta float64 } // Projectile.Tick is called every game tick and moves projectiles along, @@ -98,7 +98,7 @@ func (p *Projectile) Tick(g *Game) { for player := range g.players { for _, r := range player.Robots { dist := v.Distance(r.Position, p.Position) - if dist < float32(p.Radius) { + if dist < float64(p.Radius) { if r.Health > 0 { r.Health -= p.Damage r.Hit = true diff --git a/protocol.go b/protocol.go index 7653536..fa2f2d1 100644 --- a/protocol.go +++ b/protocol.go @@ -71,8 +71,8 @@ func (config ClientConfig) Valid(max int) bool { // BoardSize is the response containing the geometry of the requested game. type BoardSize struct { - Width float32 `json:"width"` - Height float32 `json:"height"` + Width float64 `json:"width"` + Height float64 `json:"height"` } // GameParam is sent to the client to tell them of the geometry of the game @@ -175,8 +175,8 @@ func (c *Controller) AddPlayer(ws *websocket.Conn) { var err error game, err = NewGame( gid.Id, - float32(c.Conf.Width), - float32(c.Conf.Height), + float64(c.Conf.Width), + float64(c.Conf.Height), c.Conf.ObstacleCount, c.Conf.Tick, c.Conf.MaxPoints, diff --git a/robot.go b/robot.go index cde8316..20f71ee 100644 --- a/robot.go +++ b/robot.go @@ -15,11 +15,11 @@ type Robot struct { Name string `json:"name"` Message string `json:"-"` Stats Stats `json:"-"` - TargetSpeed float32 `json:"-"` - Speed float32 `json:"speed"` + TargetSpeed float64 `json:"-"` + Speed float64 `json:"speed"` Health int `json:"health"` - RepairCounter float32 `json:"repair"` - ScanCounter float32 `json:"scan_bonus"` + RepairCounter float64 `json:"repair"` + ScanCounter float64 `json:"scan_bonus"` ActiveScan bool `json:"-"` Position v.Point2d `json:"position"` Heading v.Vector2d `json:"heading"` @@ -33,7 +33,7 @@ type Robot struct { Probe *v.Point2d `json:"probe"` ProbeResult *Collision `json:"probe_result"` gameStats *BotStats `json:"-"` - Delta float32 `json:"-"` + Delta float64 `json:"-"` idg *IdGenerator } @@ -101,14 +101,14 @@ func (s AllRobotSorter) Less(i, j int) bool { // Stats is the point allocation for a Robot. type Stats struct { Hp int `json:"hp"` - Speed float32 `json:"speed"` - Acceleration float32 `json:"acceleration"` + Speed float64 `json:"speed"` + Acceleration float64 `json:"acceleration"` WeaponRadius int `json:"weapon_radius"` ScannerRadius int `json:"scanner_radius"` TurnSpeed int `json:"turn_speed"` FireRate int `json:"fire_rate"` WeaponDamage int `json:"weapon_damage"` - WeaponSpeed float32 `json:"weapon_speed"` + WeaponSpeed float64 `json:"weapon_speed"` } // StatsRequest is the struct used in comunication with the player. We request @@ -131,41 +131,41 @@ func DeriveStats(request StatsRequest) Stats { s := Stats{} // Conversion Tables - 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) + hp_min := 20.0 + hp_max := 200.0 + s.Hp = int((float64(request.Hp) / 100.0 * (hp_max - hp_min)) + hp_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 + speed_min := 40.0 + speed_max := 200.0 + s.Speed = float64(request.Speed)/100.0*(speed_max-speed_min) + speed_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 + accel_min := 20.0 + accel_max := 200.0 + s.Acceleration = ((float64(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_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) + 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 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) + 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 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) + 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 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) + 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 weapon_damage_min float32 = 0.0 - var weapon_damage_max float32 = 20.0 - s.WeaponDamage = int(((float32(request.WeaponDamage) / 100.0) * (weapon_damage_max - weapon_damage_min)) + weapon_damage_min) + weapon_damage_min := 0.0 + weapon_damage_max := 20.0 + s.WeaponDamage = int(((float64(request.WeaponDamage) / 100.0) * (weapon_damage_max - weapon_damage_min)) + weapon_damage_min) - var weapon_speed_min float32 = 80.0 - var weapon_speed_max float32 = 600.0 - s.WeaponSpeed = float32(((float32(request.WeaponSpeed) / 100.0) * (weapon_speed_max - weapon_speed_min)) + weapon_speed_min) + weapon_speed_min := 80.0 + weapon_speed_max := 600.0 + s.WeaponSpeed = float64(((float64(request.WeaponSpeed) / 100.0) * (weapon_speed_max - weapon_speed_min)) + weapon_speed_min) return s } @@ -177,7 +177,7 @@ type Instruction struct { Heading *v.Vector2d `json:"heading,omitempty"` FireAt *v.Point2d `json:"fire_at,omitempty"` Probe *v.Point2d `json:"probe,omitempty"` - TargetSpeed *float32 `json:"target_speed,omitempty"` + TargetSpeed *float64 `json:"target_speed,omitempty"` Repair *bool `json:"repair,omitempty"` Scan *bool `json:"scan,omitempty"` } @@ -187,12 +187,12 @@ type Instruction struct { func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *Robot) { finalCollision := false collision := false - closest := float32(math.Inf(1)) + closest := math.Inf(1) var intersection *v.Point2d var finalRobot *Robot // TODO: this needs moved to the conf? - botSize := float32(5.0) + botSize := 5.0 botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize) // Check Walls @@ -314,12 +314,12 @@ func (r *Robot) Tick(g *Game) { } // Max turn radius in this case is in degrees per second - if float32(math.Abs(float64(angle))) > (float32(r.Stats.TurnSpeed) * r.Delta) { + if math.Abs(angle) > float64(r.Stats.TurnSpeed)*r.Delta { // New heading should be a little less, take current heading and // rotate by the max turn radius per frame. - rot := (float32(r.Stats.TurnSpeed) * r.Delta) * v.Deg2rad + rot := (float64(r.Stats.TurnSpeed) * r.Delta) * v.Deg2rad - new_heading = current_heading.Rotate(rot * float32(dir)) + new_heading = current_heading.Rotate(rot * dir) } move_vector := new_heading.Scale(r.Speed * r.Delta) @@ -386,9 +386,9 @@ func (r *Robot) Tick(g *Game) { // We are only allowed to scan when we're stopped if math.Abs(float64(r.Speed)) < v.Epsilon && r.ActiveScan { - r.ScanCounter += r.Delta * float32(r.Stats.ScannerRadius) * 0.1 + r.ScanCounter += r.Delta * float64(r.Stats.ScannerRadius) * 0.1 } else if r.ScanCounter > 0 { - r.ScanCounter -= r.Delta * float32(r.Stats.ScannerRadius) * 0.05 + r.ScanCounter -= r.Delta * float64(r.Stats.ScannerRadius) * 0.05 if r.ScanCounter <= 0 { r.ScanCounter = 0 } @@ -425,7 +425,7 @@ func (r *Robot) scan(g *Game) { continue } dist := v.Distance(bot.Position, r.Position) - if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) { + if dist < float64(r.Stats.ScannerRadius+int(r.ScanCounter)) { s := Scanner{ Id: bot.Id, Type: "robot", @@ -441,7 +441,7 @@ func (r *Robot) scan(g *Game) { } dist := v.Distance(proj.Position, r.Position) - if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) { + if dist < float64(r.Stats.ScannerRadius+int(r.ScanCounter)) { s := Scanner{ Id: proj.Id, Type: "projectile", @@ -452,7 +452,7 @@ 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)) { + if dist < float64(r.Stats.ScannerRadius+int(r.ScanCounter)) { s := Scanner{ Id: splo.Id, Type: "explosion", @@ -466,8 +466,8 @@ func (r *Robot) scan(g *Game) { // fire is called according to player instruction. XXX: There is a race here... func (r *Robot) fire(g *Game) *Projectile { // Throttle the fire rate - time_since_fired := (float32(g.turn) * (r.Delta * 1000)) - (float32(r.LastFired) * (r.Delta * 1000)) - if time_since_fired < float32(r.Stats.FireRate) { + time_since_fired := (float64(g.turn) * (r.Delta * 1000)) - (float64(r.LastFired) * (r.Delta * 1000)) + if time_since_fired < float64(r.Stats.FireRate) { return nil } @@ -490,8 +490,8 @@ func (r *Robot) fire(g *Game) *Projectile { func (r *Robot) reset(g *Game) { for { start_pos := v.Point2d{ - X: rand.Float32() * float32(g.width), - Y: rand.Float32() * float32(g.height), + X: rand.Float64() * float64(g.width), + Y: rand.Float64() * float64(g.height), } r.MoveTo = &start_pos r.Position = start_pos