use float64

This commit is contained in:
Ilia Denisov
2025-09-23 22:27:09 +03:00
parent 1a1bd0af17
commit 7d93176031
9 changed files with 60 additions and 60 deletions
+2 -2
View File
@@ -84,9 +84,9 @@ func (p Bitmap) SetFreeN(number int) error {
return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n) return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n)
} }
func (p Bitmap) Circle(x, y int, r float32, fill bool) { func (p Bitmap) Circle(x, y int, r float64, fill bool) {
plotX := 0 plotX := 0
plotY := int(math.Ceil(float64(r))) plotY := int(math.Ceil(r))
delta := 3 - 2*plotY delta := 3 - 2*plotY
lastY := plotY lastY := plotY
for plotX <= plotY { for plotX <= plotY {
+1 -1
View File
@@ -221,7 +221,7 @@ func TestClear(t *testing.T) {
func TestCircle(t *testing.T) { func TestCircle(t *testing.T) {
type testCase struct { type testCase struct {
x, y int x, y int
r float32 r float64
filled bool filled bool
} }
for i, tc := range []testCase{ for i, tc := range []testCase{
+6 -6
View File
@@ -22,7 +22,7 @@ func Generate(cfg ...func(*MapSetting)) (Map, error) {
freePlanets := ms.NobodysPlanets() freePlanets := ms.NobodysPlanets()
createPlanets := func(ps PlanetSetting) error { createPlanets := func(ps PlanetSetting) error {
return m.CreatePlanets(ps.Count(freePlanets), float32(ps.MinDistanceHW), RandIFn(ps.MinSize, ps.MaxSize), RandIFn(ps.MinResource, ps.MaxResource)) return m.CreatePlanets(ps.Count(freePlanets), float64(ps.MinDistanceHW), RandIFn(ps.MinSize, ps.MaxSize), RandIFn(ps.MinResource, ps.MaxResource))
} }
// 1. Place Giant planets // 1. Place Giant planets
@@ -37,18 +37,18 @@ func Generate(cfg ...func(*MapSetting)) (Map, error) {
// 3. Place players' Home Worlds // 3. Place players' Home Worlds
for player := 0; player < int(ms.Players); player++ { for player := 0; player < int(ms.Players); player++ {
coord, err := m.NewCoordinate(float32(ms.HWMinDistance)) coord, err := m.NewCoordinate(float64(ms.HWMinDistance))
if err != nil { if err != nil {
return Map{}, fmt.Errorf("%s: hw new_coordinate: %s", ms, err) return Map{}, fmt.Errorf("%s: hw new_coordinate: %s", ms, err)
} }
planet := NewPlanet(coord, float32(ms.HWSize), float32(ms.HWResources)) planet := NewPlanet(coord, float64(ms.HWSize), float64(ms.HWResources))
m.HomePlanets[player] = PlanetarySystem{HW: planet, DW: make([]Planet, ms.DWCount)} m.HomePlanets[player] = PlanetarySystem{HW: planet, DW: make([]Planet, ms.DWCount)}
for dw := 0; dw < int(ms.DWCount); dw++ { for dw := 0; dw < int(ms.DWCount); dw++ {
p := rand.Float64()*(float64(ms.DWMaxDistance)-float64(ms.DWMinDistance)) + float64(ms.DWMinDistance) p := rand.Float64()*(float64(ms.DWMaxDistance)-float64(ms.DWMinDistance)) + float64(ms.DWMinDistance)
phi := rand.Float64() * 360 phi := rand.Float64() * 360
x := float32(p * math.Cos(phi)) x := p * math.Cos(phi)
y := float32(p * math.Sin(phi)) y := p * math.Sin(phi)
dwPlanet := NewPlanet(Coordinate{x, y}, float32(ms.DWSize), float32(ms.DWResources)) dwPlanet := NewPlanet(Coordinate{x, y}, float64(ms.DWSize), float64(ms.DWResources))
m.HomePlanets[player].DW[dw] = dwPlanet m.HomePlanets[player].DW[dw] = dwPlanet
} }
} }
+11 -11
View File
@@ -16,13 +16,13 @@ type Map struct {
} }
type Coordinate struct { type Coordinate struct {
X, Y float32 X, Y float64
} }
type Planet struct { type Planet struct {
Position Coordinate Position Coordinate
Size float32 Size float64
Resources float32 // Сырьё Resources float64 // Сырьё
} }
type PlanetarySystem struct { type PlanetarySystem struct {
@@ -43,7 +43,7 @@ func NewMap(width, height, players uint32) (*Map, error) {
}, nil }, nil
} }
func (m *Map) CreatePlanets(num int, deadZoneRadius float32, size, resources func() float32) error { func (m *Map) CreatePlanets(num int, deadZoneRadius float64, size, resources func() float64) error {
for range num { for range num {
coord, err := m.NewCoordinate(deadZoneRadius) coord, err := m.NewCoordinate(deadZoneRadius)
if err != nil { if err != nil {
@@ -59,7 +59,7 @@ func (m *Map) AddPlanet(planet Planet) {
m.FreePlanets = append(m.FreePlanets, planet) m.FreePlanets = append(m.FreePlanets, planet)
} }
func (m Map) NewCoordinate(deadZoneRaduis float32) (Coordinate, error) { func (m Map) NewCoordinate(deadZoneRaduis float64) (Coordinate, error) {
if x, y, err := m.plotter.RandomFreePoint(deadZoneRaduis); err != nil { if x, y, err := m.plotter.RandomFreePoint(deadZoneRaduis); err != nil {
return Coordinate{}, fmt.Errorf("NewCoordinate: RandomFreePoint: %s", err) return Coordinate{}, fmt.Errorf("NewCoordinate: RandomFreePoint: %s", err)
} else { } else {
@@ -67,7 +67,7 @@ func (m Map) NewCoordinate(deadZoneRaduis float32) (Coordinate, error) {
} }
} }
func NewPlanet(c Coordinate, size, resources float32) Planet { func NewPlanet(c Coordinate, size, resources float64) Planet {
return Planet{ return Planet{
Position: c, Position: c,
Size: size, Size: size,
@@ -75,14 +75,14 @@ func NewPlanet(c Coordinate, size, resources float32) Planet {
} }
} }
// RandI returns a random float32 value between min and max // RandI returns a random float64 value between min and max
func RandI(min, max float32) float32 { func RandI(min, max float64) float64 {
return min + rand.Float32()*(max-min) return min + rand.Float64()*(max-min)
} }
// RandIFn is a wrapper for the [RandI] func // RandIFn is a wrapper for the [RandI] func
func RandIFn(min, max float32) func() float32 { func RandIFn(min, max float64) func() float64 {
return func() float32 { return func() float64 {
return RandI(min, max) return RandI(min, max)
} }
} }
+12 -12
View File
@@ -10,35 +10,35 @@ import (
) )
type Plotter struct { type Plotter struct {
factor float32 factor float64
clearFn func() clearFn func()
circleFn func(x, y int, r float32) circleFn func(x, y int, r float64)
freeCountFn func() int freeCountFn func() int
freeNumberToCoordFn func(int) (int, int, error) freeNumberToCoordFn func(int) (int, int, error)
} }
func NewPlotter(width, height uint32, factor float32) (Plotter, error) { func NewPlotter(width, height uint32, factor float64) (Plotter, error) {
return NewBitmapPlotter(NewBitmap(width, height, factor), factor) return NewBitmapPlotter(NewBitmap(width, height, factor), factor)
} }
func NewBitmap(width, height uint32, factor float32) bitmap.Bitmap { func NewBitmap(width, height uint32, factor float64) bitmap.Bitmap {
return bitmap.NewBitmap(AsPlotterSize(width, height, factor)) return bitmap.NewBitmap(AsPlotterSize(width, height, factor))
} }
func NewBitmapPlotter(bm bitmap.Bitmap, factor float32) (Plotter, error) { func NewBitmapPlotter(bm bitmap.Bitmap, factor float64) (Plotter, error) {
if factor > 1 || factor <= 0 { if factor > 1 || factor <= 0 {
return Plotter{}, fmt.Errorf("factor should be: 0 > F <= 1") return Plotter{}, fmt.Errorf("factor should be: 0 > F <= 1")
} }
return Plotter{ return Plotter{
factor: factor, factor: factor,
clearFn: bm.Clear, clearFn: bm.Clear,
circleFn: func(x, y int, r float32) { bm.Circle(x, y, r, true) }, circleFn: func(x, y int, r float64) { bm.Circle(x, y, r, true) },
freeCountFn: bm.FreeCount, freeCountFn: bm.FreeCount,
freeNumberToCoordFn: bm.GetFreeN, freeNumberToCoordFn: bm.GetFreeN,
}, nil }, nil
} }
func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, error) { func (p Plotter) RandomFreePoint(deadZoneRaduis float64) (float64, float64, error) {
fsCount := p.freeCountFn() fsCount := p.freeCountFn()
if fsCount == 0 { if fsCount == 0 {
return 0, 0, errors.New("RandomFreePoint: no free space left") return 0, 0, errors.New("RandomFreePoint: no free space left")
@@ -51,22 +51,22 @@ func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, erro
if deadZoneRaduis > 0 { if deadZoneRaduis > 0 {
p.plotDeadZone(x, y, deadZoneRaduis) p.plotDeadZone(x, y, deadZoneRaduis)
} }
planetX := float32(x)*p.factor + rand.Float32()*p.factor planetX := float64(x)*p.factor + rand.Float64()*p.factor
planetY := float32(y)*p.factor + rand.Float32()*p.factor planetY := float64(y)*p.factor + rand.Float64()*p.factor
return planetX, planetY, nil return planetX, planetY, nil
} }
func (p Plotter) MarkDeadZone(x, y float32, radius float32) { func (p Plotter) MarkDeadZone(x, y float64, radius float64) {
p.plotDeadZone(int(x/p.factor), int(y/p.factor), radius) p.plotDeadZone(int(x/p.factor), int(y/p.factor), radius)
} }
func (p Plotter) plotDeadZone(x, y int, radius float32) { func (p Plotter) plotDeadZone(x, y int, radius float64) {
p.circleFn(x, y, radius/p.factor) p.circleFn(x, y, radius/p.factor)
} }
func (p Plotter) Clear() { p.clearFn() } func (p Plotter) Clear() { p.clearFn() }
func AsPlotterSize(width, height uint32, factor float32) (uint32, uint32) { func AsPlotterSize(width, height uint32, factor float64) (uint32, uint32) {
if factor > 1 || factor <= 0 { if factor > 1 || factor <= 0 {
return width, height return width, height
} }
+4 -4
View File
@@ -28,7 +28,7 @@ func TestNewPlotter(t *testing.T) {
func TestAsPlotterSize(t *testing.T) { func TestAsPlotterSize(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
w, h uint32 w, h uint32
f float32 f float64
ew, eh uint32 ew, eh uint32
}{ }{
{10, 10, 0, 10, 10}, {10, 10, 0, 10, 10},
@@ -46,7 +46,7 @@ func TestAsPlotterSize(t *testing.T) {
} }
func TestRandomFreePoint(t *testing.T) { func TestRandomFreePoint(t *testing.T) {
var factor float32 = 0.25 var factor float64 = 0.25
var w, h uint32 = 20, 20 var w, h uint32 = 20, 20
bm := plotter.NewBitmap(w, h, factor) bm := plotter.NewBitmap(w, h, factor)
p, err := plotter.NewBitmapPlotter(bm, factor) // 80x80 p, err := plotter.NewBitmapPlotter(bm, factor) // 80x80
@@ -58,7 +58,7 @@ func TestRandomFreePoint(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("expect: no error getting random point, got: %s", err) t.Errorf("expect: no error getting random point, got: %s", err)
} }
if x > float32(w) || y > float32(w) { if x > float64(w) || y > float64(w) {
t.Errorf("expect: point coordinates within map size %dx%d, got: x=%f y=%f", w, h, x, y) t.Errorf("expect: point coordinates within map size %dx%d, got: x=%f y=%f", w, h, x, y)
} }
@@ -67,7 +67,7 @@ func TestRandomFreePoint(t *testing.T) {
t.Errorf("expect: no error when radius is zero, got: %s", err) t.Errorf("expect: no error when radius is zero, got: %s", err)
} }
_, _, err = p.RandomFreePoint(float32(w + h)) // guaranteed to mark whole area dead zone _, _, err = p.RandomFreePoint(float64(w + h)) // guaranteed to mark whole area dead zone
if err != nil { if err != nil {
t.Errorf("expect: no error getting random point, got: %s", err) t.Errorf("expect: no error getting random point, got: %s", err)
} }
+7 -7
View File
@@ -5,7 +5,7 @@ import (
"math" "math"
) )
const defaultFactor float32 = 0.1 const defaultFactor float64 = 0.1
type MapSetting struct { type MapSetting struct {
Players uint32 Players uint32
@@ -20,7 +20,7 @@ type MapSetting struct {
GiantPlanets PlanetSetting GiantPlanets PlanetSetting
BigPlanets PlanetSetting BigPlanets PlanetSetting
OthersMinDistance float32 OthersMinDistance float64
NormalPlanets PlanetSetting NormalPlanets PlanetSetting
RichPlanets PlanetSetting RichPlanets PlanetSetting
Asterioids PlanetSetting Asterioids PlanetSetting
@@ -44,11 +44,11 @@ func (ms MapSetting) NobodysPlanets() uint32 {
type PlanetSetting struct { type PlanetSetting struct {
MinDistanceHW uint32 MinDistanceHW uint32
MinSize float32 MinSize float64
MaxSize float32 MaxSize float64
MinResource float32 MinResource float64
MaxResource float32 MaxResource float64
Probability float32 Probability float64
} }
func (ps PlanetSetting) Count(freePlanets uint32) int { func (ps PlanetSetting) Count(freePlanets uint32) int {
+10 -10
View File
@@ -3,26 +3,26 @@ package game
import "math" import "math"
type Planet struct { type Planet struct {
X, Y float32 X, Y float64
Size float32 Size float64
Name string Name string
Owner string Owner string
Production ProductionType Production ProductionType
Resources float32 // Сырьё Resources float64 // Сырьё
Industry float32 // Промышленность Industry float64 // Промышленность
Population float32 // Население Population float64 // Население
Capital float32 // CAP $ - Запасы промышленности Capital float64 // CAP $ - Запасы промышленности
Material float32 // MAT M - Запасы сырья Material float64 // MAT M - Запасы сырья
Colonists float32 // COL C - Количество колонистов Colonists float64 // COL C - Количество колонистов
// Параметр "L" означает количество свободных производственных единиц. // Параметр "L" означает количество свободных производственных единиц.
} }
// Производственный потенциал (I) // Производственный потенциал (I)
// промышленность * 0.75 + население * 0.25 // промышленность * 0.75 + население * 0.25
func (p Planet) ProductionCapacity() float32 { func (p Planet) ProductionCapacity() float64 {
return p.Industry*0.75 + p.Population*0.25 return p.Industry*0.75 + p.Population*0.25
} }
@@ -30,7 +30,7 @@ func (p Planet) ProductionCapacity() float32 {
// TODO: test on real values // TODO: test on real values
func (p *Planet) IncreaseIndustry() { func (p *Planet) IncreaseIndustry() {
prod := p.ProductionCapacity() / 5 prod := p.ProductionCapacity() / 5
industryIncrement := float32(math.Min(float64(prod), float64(p.Material))) industryIncrement := math.Min(prod, p.Material)
p.Industry += industryIncrement p.Industry += industryIncrement
if p.Industry > p.Population { if p.Industry > p.Population {
p.Industry = p.Population p.Industry = p.Population
+7 -7
View File
@@ -4,19 +4,19 @@ type Race struct {
Name string Name string
Killed bool Killed bool
Votes float32 Votes float64
VoteFor string VoteFor string
Drive float32 Drive float64
Weapons float32 Weapons float64
Shields float32 Shields float64
Cargo float32 Cargo float64
} }
func (r Race) FlightDistance() float32 { func (r Race) FlightDistance() float64 {
return r.Drive * 40 return r.Drive * 40
} }
func (r Race) VisibilityDistance() float32 { func (r Race) VisibilityDistance() float64 {
return r.Drive * 30 return r.Drive * 30
} }