Browse Source

float32 -> float64

Stephen McQuay 5 years ago
parent
commit
8f7ef5aa1a
8 changed files with 83 additions and 82 deletions
  1. 11
    10
      config.go
  2. 3
    3
      control.go
  3. 4
    4
      game.go
  4. 7
    7
      obstacle.go
  5. 1
    1
      player.go
  6. 3
    3
      projectile.go
  7. 4
    4
      protocol.go
  8. 50
    50
      robot.go

+ 11
- 10
config.go View File

@@ -15,14 +15,15 @@ import (
15 15
 // ways (json POSTed to server, config file, constants) and are typically owned
16 16
 // by a Controller.
17 17
 type Config struct {
18
-	Tick          int     `json:"tick"` // ms
19
-	Timescale     float32 `json:"timescale"`
20
-	Delta         float32 `json:"delta"`
21
-	Width         int     `json:"width"`
22
-	Height        int     `json:"height"`
23
-	ObstacleCount int     `json:"obstacle_count"`
24
-	MaxPoints     int     `json:"max_points"`
25
-	Mode          string  `json:"mode"`
18
+	Tick          int        `json:"tick"` // ms
19
+	Timescale     float64    `json:"timescale"`
20
+	Delta         float64    `json:"delta"`
21
+	Width         int        `json:"width"`
22
+	Height        int        `json:"height"`
23
+	ObstacleCount int        `json:"obstacle_count"`
24
+	Obstacles     []Obstacle `json:'obstacles'`
25
+	MaxPoints     int        `json:"max_points"`
26
+	Mode          string     `json:"mode"`
26 27
 }
27 28
 
28 29
 const (
@@ -69,7 +70,7 @@ func LoadConfig(filename string) (Config, error) {
69 70
 			return c, errors.New(fmt.Sprintf("config parse error: %s", err))
70 71
 		}
71 72
 	}
72
-	c.Delta = (float32(c.Tick) / 1000.0) * float32(c.Timescale)
73
-	log.Printf("final config: %+v", c)
73
+	c.Delta = float64(c.Tick) / 1000.0 * c.Timescale
74
+	log.Printf("final config: %#v", c)
74 75
 	return c, nil
75 76
 }

+ 3
- 3
control.go View File

@@ -62,7 +62,7 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) {
62 62
 	log.Println("asked to create a game")
63 63
 
64 64
 	requested_game_name := c.Idg.Hash()
65
-	width, height := float32(c.Conf.Width), float32(c.Conf.Height)
65
+	width, height := float64(c.Conf.Width), float64(c.Conf.Height)
66 66
 	obstacleCount := 0
67 67
 	maxPoints := c.Conf.MaxPoints
68 68
 	mode := "deathmatch"
@@ -87,8 +87,8 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) {
87 87
 			return
88 88
 		}
89 89
 		requested_game_name = cfg.Name
90
-		width = float32(cfg.Width)
91
-		height = float32(cfg.Height)
90
+		width = float64(cfg.Width)
91
+		height = float64(cfg.Height)
92 92
 		obstacleCount = cfg.ObstacleCount
93 93
 		maxPoints = cfg.MaxPoints
94 94
 		mode = cfg.Mode

+ 4
- 4
game.go View File

@@ -65,14 +65,14 @@ type Game struct {
65 65
 	unregister        chan *Player
66 66
 	turn              int
67 67
 	players_remaining int
68
-	width, height     float32
68
+	width, height     float64
69 69
 	maxPoints         int
70 70
 	spectators        map[*Spectator]bool
71 71
 	sregister         chan *Spectator
72 72
 	sunregister       chan *Spectator
73 73
 	kill              chan bool
74 74
 	repair_hp         int
75
-	repair_rate       float32
75
+	repair_rate       float64
76 76
 	tick_duration     int
77 77
 	stats             GameStats
78 78
 	mode              GameMode
@@ -87,7 +87,7 @@ type GameMode interface {
87 87
 }
88 88
 
89 89
 // NewGame Poplulates a Game struct and starts the bandwidth calculator.
90
-func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) (*Game, error) {
90
+func NewGame(id string, width, height float64, obstacles, tick, maxPoints int, mode string) (*Game, error) {
91 91
 	bw, err := bandwidth.NewBandwidth(
92 92
 		[]int{1, 10, 60},
93 93
 		1*time.Second,
@@ -263,7 +263,7 @@ func (g *Game) sendUpdate(payload *Boardstate) {
263 263
 
264 264
 				// Filter objects
265 265
 				for _, ob := range g.obstacles {
266
-					if ob.distance_from_point(r.Position) < float32(r.Stats.ScannerRadius)+r.ScanCounter {
266
+					if ob.distance_from_point(r.Position) < float64(r.Stats.ScannerRadius)+r.ScanCounter {
267 267
 						player_payload.Objects = append(
268 268
 							player_payload.Objects, ob.minify())
269 269
 					}

+ 7
- 7
obstacle.go View File

@@ -13,7 +13,7 @@ type Obstacle struct {
13 13
 	Hp     int      `json:"-"`
14 14
 }
15 15
 
16
-func (o Obstacle) distance_from_point(p v.Point2d) float32 {
16
+func (o Obstacle) distance_from_point(p v.Point2d) float64 {
17 17
 	dist := math.MaxFloat32
18 18
 
19 19
 	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 {
21 21
 	dist = math.Min(dist, float64(p.Sub(v.Point2d{X: o.Bounds.A.X, Y: o.Bounds.B.Y}).Mag()))
22 22
 	dist = math.Min(dist, float64(p.Sub(v.Point2d{X: o.Bounds.B.X, Y: o.Bounds.A.Y}).Mag()))
23 23
 
24
-	return float32(dist)
24
+	return dist
25 25
 }
26 26
 
27 27
 func (o Obstacle) minify() [4]int {
@@ -41,13 +41,13 @@ func MinifyObstacles(o []Obstacle) [][4]int {
41 41
 
42 42
 // GenerateObstacles returns a slice of (count) obstacles within a region
43 43
 // bounded by width, height.
44
-func GenerateObstacles(count int, width, height float32) []Obstacle {
44
+func GenerateObstacles(count int, width, height float64) []Obstacle {
45 45
 	out := []Obstacle{}
46 46
 	for i := 0; i < count; i++ {
47
-		x := rand.Float32() * (width - (width / 10))
48
-		y := rand.Float32() * (height - (height / 10))
49
-		w := rand.Float32() * (width / 10)
50
-		h := rand.Float32() * (height / 10)
47
+		x := rand.Float64() * (width - (width / 10))
48
+		y := rand.Float64() * (height - (height / 10))
49
+		w := rand.Float64() * (width / 10)
50
+		h := rand.Float64() * (height / 10)
51 51
 		out = append(
52 52
 			out,
53 53
 			Obstacle{

+ 1
- 1
player.go View File

@@ -159,7 +159,7 @@ func (p *Player) Recv() {
159 159
 				}
160 160
 
161 161
 				if msg.TargetSpeed != nil {
162
-					r.TargetSpeed = float32(*msg.TargetSpeed)
162
+					r.TargetSpeed = *msg.TargetSpeed
163 163
 				} else {
164 164
 					r.TargetSpeed = r.Stats.Speed
165 165
 				}

+ 3
- 3
projectile.go View File

@@ -12,10 +12,10 @@ type Projectile struct {
12 12
 	Position v.Point2d `json:"position"`
13 13
 	MoveTo   v.Point2d `json:"-"`
14 14
 	Radius   int       `json:"-"`
15
-	Speed    float32   `json:"-"`
15
+	Speed    float64   `json:"-"`
16 16
 	Damage   int       `json:"-"`
17 17
 	Owner    *Robot    `json:"-"`
18
-	Delta    float32
18
+	Delta    float64
19 19
 }
20 20
 
21 21
 // Projectile.Tick is called every game tick and moves projectiles along,
@@ -98,7 +98,7 @@ func (p *Projectile) Tick(g *Game) {
98 98
 		for player := range g.players {
99 99
 			for _, r := range player.Robots {
100 100
 				dist := v.Distance(r.Position, p.Position)
101
-				if dist < float32(p.Radius) {
101
+				if dist < float64(p.Radius) {
102 102
 					if r.Health > 0 {
103 103
 						r.Health -= p.Damage
104 104
 						r.Hit = true

+ 4
- 4
protocol.go View File

@@ -71,8 +71,8 @@ func (config ClientConfig) Valid(max int) bool {
71 71
 
72 72
 // BoardSize is the response containing the geometry of the requested game.
73 73
 type BoardSize struct {
74
-	Width  float32 `json:"width"`
75
-	Height float32 `json:"height"`
74
+	Width  float64 `json:"width"`
75
+	Height float64 `json:"height"`
76 76
 }
77 77
 
78 78
 // 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) {
175 175
 		var err error
176 176
 		game, err = NewGame(
177 177
 			gid.Id,
178
-			float32(c.Conf.Width),
179
-			float32(c.Conf.Height),
178
+			float64(c.Conf.Width),
179
+			float64(c.Conf.Height),
180 180
 			c.Conf.ObstacleCount,
181 181
 			c.Conf.Tick,
182 182
 			c.Conf.MaxPoints,

+ 50
- 50
robot.go View File

@@ -15,11 +15,11 @@ type Robot struct {
15 15
 	Name           string      `json:"name"`
16 16
 	Message        string      `json:"-"`
17 17
 	Stats          Stats       `json:"-"`
18
-	TargetSpeed    float32     `json:"-"`
19
-	Speed          float32     `json:"speed"`
18
+	TargetSpeed    float64     `json:"-"`
19
+	Speed          float64     `json:"speed"`
20 20
 	Health         int         `json:"health"`
21
-	RepairCounter  float32     `json:"repair"`
22
-	ScanCounter    float32     `json:"scan_bonus"`
21
+	RepairCounter  float64     `json:"repair"`
22
+	ScanCounter    float64     `json:"scan_bonus"`
23 23
 	ActiveScan     bool        `json:"-"`
24 24
 	Position       v.Point2d   `json:"position"`
25 25
 	Heading        v.Vector2d  `json:"heading"`
@@ -33,7 +33,7 @@ type Robot struct {
33 33
 	Probe          *v.Point2d  `json:"probe"`
34 34
 	ProbeResult    *Collision  `json:"probe_result"`
35 35
 	gameStats      *BotStats   `json:"-"`
36
-	Delta          float32     `json:"-"`
36
+	Delta          float64     `json:"-"`
37 37
 	idg            *IdGenerator
38 38
 }
39 39
 
@@ -101,14 +101,14 @@ func (s AllRobotSorter) Less(i, j int) bool {
101 101
 // Stats is the point allocation for a Robot.
102 102
 type Stats struct {
103 103
 	Hp            int     `json:"hp"`
104
-	Speed         float32 `json:"speed"`
105
-	Acceleration  float32 `json:"acceleration"`
104
+	Speed         float64 `json:"speed"`
105
+	Acceleration  float64 `json:"acceleration"`
106 106
 	WeaponRadius  int     `json:"weapon_radius"`
107 107
 	ScannerRadius int     `json:"scanner_radius"`
108 108
 	TurnSpeed     int     `json:"turn_speed"`
109 109
 	FireRate      int     `json:"fire_rate"`
110 110
 	WeaponDamage  int     `json:"weapon_damage"`
111
-	WeaponSpeed   float32 `json:"weapon_speed"`
111
+	WeaponSpeed   float64 `json:"weapon_speed"`
112 112
 }
113 113
 
114 114
 // StatsRequest is the struct used in comunication with the player. We request
@@ -131,41 +131,41 @@ func DeriveStats(request StatsRequest) Stats {
131 131
 	s := Stats{}
132 132
 
133 133
 	// Conversion Tables
134
-	var hp_min float32 = 20.0
135
-	var hp_max float32 = 200.0
136
-	s.Hp = int(((float32(request.Hp) / 100.0) * (hp_max - hp_min)) + hp_min)
134
+	hp_min := 20.0
135
+	hp_max := 200.0
136
+	s.Hp = int((float64(request.Hp) / 100.0 * (hp_max - hp_min)) + hp_min)
137 137
 
138
-	var speed_min float32 = 40.0
139
-	var speed_max float32 = 200.0
140
-	s.Speed = ((float32(request.Speed) / 100.0) * (speed_max - speed_min)) + speed_min
138
+	speed_min := 40.0
139
+	speed_max := 200.0
140
+	s.Speed = float64(request.Speed)/100.0*(speed_max-speed_min) + speed_min
141 141
 
142
-	var accel_min float32 = 20.0
143
-	var accel_max float32 = 200.0
144
-	s.Acceleration = ((float32(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min
142
+	accel_min := 20.0
143
+	accel_max := 200.0
144
+	s.Acceleration = ((float64(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min
145 145
 
146
-	var wep_rad_min float32 = 5.0
147
-	var wep_rad_max float32 = 60.0
148
-	s.WeaponRadius = int(((float32(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min)
146
+	wep_rad_min := 5.0
147
+	wep_rad_max := 60.0
148
+	s.WeaponRadius = int(((float64(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min)
149 149
 
150
-	var scan_rad_min float32 = 100.0
151
-	var scan_rad_max float32 = 400.0
152
-	s.ScannerRadius = int(((float32(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min)
150
+	scan_rad_min := 100.0
151
+	scan_rad_max := 400.0
152
+	s.ScannerRadius = int(((float64(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min)
153 153
 
154
-	var turn_spd_min float32 = 30.0
155
-	var turn_spd_max float32 = 300.0
156
-	s.TurnSpeed = int(((float32(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min)
154
+	turn_spd_min := 30.0
155
+	turn_spd_max := 300.0
156
+	s.TurnSpeed = int(((float64(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min)
157 157
 
158
-	var fire_rate_min float32 = 10.0
159
-	var fire_rate_max float32 = 2000.0
160
-	s.FireRate = int(fire_rate_max+300.0) - int(((float32(request.FireRate)/100.0)*(fire_rate_max-fire_rate_min))+fire_rate_min)
158
+	fire_rate_min := 10.0
159
+	fire_rate_max := 2000.0
160
+	s.FireRate = int(fire_rate_max+300.0) - int(((float64(request.FireRate)/100.0)*(fire_rate_max-fire_rate_min))+fire_rate_min)
161 161
 
162
-	var weapon_damage_min float32 = 0.0
163
-	var weapon_damage_max float32 = 20.0
164
-	s.WeaponDamage = int(((float32(request.WeaponDamage) / 100.0) * (weapon_damage_max - weapon_damage_min)) + weapon_damage_min)
162
+	weapon_damage_min := 0.0
163
+	weapon_damage_max := 20.0
164
+	s.WeaponDamage = int(((float64(request.WeaponDamage) / 100.0) * (weapon_damage_max - weapon_damage_min)) + weapon_damage_min)
165 165
 
166
-	var weapon_speed_min float32 = 80.0
167
-	var weapon_speed_max float32 = 600.0
168
-	s.WeaponSpeed = float32(((float32(request.WeaponSpeed) / 100.0) * (weapon_speed_max - weapon_speed_min)) + weapon_speed_min)
166
+	weapon_speed_min := 80.0
167
+	weapon_speed_max := 600.0
168
+	s.WeaponSpeed = float64(((float64(request.WeaponSpeed) / 100.0) * (weapon_speed_max - weapon_speed_min)) + weapon_speed_min)
169 169
 
170 170
 	return s
171 171
 }
@@ -177,7 +177,7 @@ type Instruction struct {
177 177
 	Heading     *v.Vector2d `json:"heading,omitempty"`
178 178
 	FireAt      *v.Point2d  `json:"fire_at,omitempty"`
179 179
 	Probe       *v.Point2d  `json:"probe,omitempty"`
180
-	TargetSpeed *float32    `json:"target_speed,omitempty"`
180
+	TargetSpeed *float64    `json:"target_speed,omitempty"`
181 181
 	Repair      *bool       `json:"repair,omitempty"`
182 182
 	Scan        *bool       `json:"scan,omitempty"`
183 183
 }
@@ -187,12 +187,12 @@ type Instruction struct {
187 187
 func (r *Robot) checkCollisions(g *Game, probe v.Vector2d) (bool, *v.Point2d, *Robot) {
188 188
 	finalCollision := false
189 189
 	collision := false
190
-	closest := float32(math.Inf(1))
190
+	closest := math.Inf(1)
191 191
 	var intersection *v.Point2d
192 192
 	var finalRobot *Robot
193 193
 
194 194
 	// TODO: this needs moved to the conf?
195
-	botSize := float32(5.0)
195
+	botSize := 5.0
196 196
 	botPolygon := v.OrientedSquare(r.Position, r.Heading, botSize)
197 197
 
198 198
 	// Check Walls
@@ -314,12 +314,12 @@ func (r *Robot) Tick(g *Game) {
314 314
 		}
315 315
 
316 316
 		// Max turn radius in this case is in degrees per second
317
-		if float32(math.Abs(float64(angle))) > (float32(r.Stats.TurnSpeed) * r.Delta) {
317
+		if math.Abs(angle) > float64(r.Stats.TurnSpeed)*r.Delta {
318 318
 			// New heading should be a little less, take current heading and
319 319
 			// rotate by the max turn radius per frame.
320
-			rot := (float32(r.Stats.TurnSpeed) * r.Delta) * v.Deg2rad
320
+			rot := (float64(r.Stats.TurnSpeed) * r.Delta) * v.Deg2rad
321 321
 
322
-			new_heading = current_heading.Rotate(rot * float32(dir))
322
+			new_heading = current_heading.Rotate(rot * dir)
323 323
 		}
324 324
 
325 325
 		move_vector := new_heading.Scale(r.Speed * r.Delta)
@@ -386,9 +386,9 @@ func (r *Robot) Tick(g *Game) {
386 386
 
387 387
 	// We are only allowed to scan when we're stopped
388 388
 	if math.Abs(float64(r.Speed)) < v.Epsilon && r.ActiveScan {
389
-		r.ScanCounter += r.Delta * float32(r.Stats.ScannerRadius) * 0.1
389
+		r.ScanCounter += r.Delta * float64(r.Stats.ScannerRadius) * 0.1
390 390
 	} else if r.ScanCounter > 0 {
391
-		r.ScanCounter -= r.Delta * float32(r.Stats.ScannerRadius) * 0.05
391
+		r.ScanCounter -= r.Delta * float64(r.Stats.ScannerRadius) * 0.05
392 392
 		if r.ScanCounter <= 0 {
393 393
 			r.ScanCounter = 0
394 394
 		}
@@ -425,7 +425,7 @@ func (r *Robot) scan(g *Game) {
425 425
 				continue
426 426
 			}
427 427
 			dist := v.Distance(bot.Position, r.Position)
428
-			if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
428
+			if dist < float64(r.Stats.ScannerRadius+int(r.ScanCounter)) {
429 429
 				s := Scanner{
430 430
 					Id:   bot.Id,
431 431
 					Type: "robot",
@@ -441,7 +441,7 @@ func (r *Robot) scan(g *Game) {
441 441
 		}
442 442
 
443 443
 		dist := v.Distance(proj.Position, r.Position)
444
-		if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
444
+		if dist < float64(r.Stats.ScannerRadius+int(r.ScanCounter)) {
445 445
 			s := Scanner{
446 446
 				Id:   proj.Id,
447 447
 				Type: "projectile",
@@ -452,7 +452,7 @@ func (r *Robot) scan(g *Game) {
452 452
 
453 453
 	for splo := range g.splosions {
454 454
 		dist := v.Distance(splo.Position, r.Position)
455
-		if dist < float32(r.Stats.ScannerRadius+int(r.ScanCounter)) {
455
+		if dist < float64(r.Stats.ScannerRadius+int(r.ScanCounter)) {
456 456
 			s := Scanner{
457 457
 				Id:   splo.Id,
458 458
 				Type: "explosion",
@@ -466,8 +466,8 @@ func (r *Robot) scan(g *Game) {
466 466
 // fire is called according to player instruction. XXX: There is a race here...
467 467
 func (r *Robot) fire(g *Game) *Projectile {
468 468
 	// Throttle the fire rate
469
-	time_since_fired := (float32(g.turn) * (r.Delta * 1000)) - (float32(r.LastFired) * (r.Delta * 1000))
470
-	if time_since_fired < float32(r.Stats.FireRate) {
469
+	time_since_fired := (float64(g.turn) * (r.Delta * 1000)) - (float64(r.LastFired) * (r.Delta * 1000))
470
+	if time_since_fired < float64(r.Stats.FireRate) {
471 471
 		return nil
472 472
 	}
473 473
 
@@ -490,8 +490,8 @@ func (r *Robot) fire(g *Game) *Projectile {
490 490
 func (r *Robot) reset(g *Game) {
491 491
 	for {
492 492
 		start_pos := v.Point2d{
493
-			X: rand.Float32() * float32(g.width),
494
-			Y: rand.Float32() * float32(g.height),
493
+			X: rand.Float64() * float64(g.width),
494
+			Y: rand.Float64() * float64(g.height),
495 495
 		}
496 496
 		r.MoveTo = &start_pos
497 497
 		r.Position = start_pos

Loading…
Cancel
Save