refactor: float64 types for storage and report

This commit is contained in:
Ilia Denisov
2026-02-02 13:14:57 +02:00
parent 4c14234afb
commit a567229f8a
24 changed files with 264 additions and 253 deletions
+13 -13
View File
@@ -107,7 +107,7 @@ type ShipGroup struct {
Number uint `json:"number"` // Number (quantity) ships of specific ShipType
CargoType *CargoType `json:"loadType,omitempty"`
Load float64 `json:"load"` // Cargo loaded - "Масса груза"
Load Float `json:"load"` // Cargo loaded - "Масса груза"
Tech TechSet `json:"tech"`
@@ -117,8 +117,8 @@ type ShipGroup struct {
StateUpgrade *InUpgrade `json:"stateUpgrade,omitempty"`
}
func (sg ShipGroup) TechLevel(t Tech) float64 {
return sg.Tech.Value(t)
func (sg ShipGroup) TechLevel(t Tech) Float {
return F(sg.Tech.Value(t))
}
// TODO: refactor to separate method with *ShipGroup as parameter
@@ -170,19 +170,19 @@ func (sg ShipGroup) Equal(other ShipGroup) bool {
sg.TechLevel(TechShields) == other.TechLevel(TechShields) &&
sg.TechLevel(TechCargo) == other.TechLevel(TechCargo) &&
sg.CargoType == other.CargoType &&
sg.Load/float64(sg.Number) == other.Load/float64(other.Number) &&
sg.Load.F()/float64(sg.Number) == other.Load.F()/float64(other.Number) &&
sg.State() == other.State()
}
// Грузоподъёмность
func (sg ShipGroup) CargoCapacity(st *ShipType) float64 {
return sg.TechLevel(TechCargo) * (st.Cargo + (st.Cargo*st.Cargo)/20) * float64(sg.Number)
return sg.TechLevel(TechCargo).F() * (st.Cargo + (st.Cargo*st.Cargo)/20) * float64(sg.Number)
}
// Масса перевозимого груза -
// общее количество единиц груза, деленное на технологический уровень Грузоперевозок
func (sg ShipGroup) CarryingMass() float64 {
return sg.Load / sg.TechLevel(TechCargo)
return sg.Load.F() / sg.TechLevel(TechCargo).F()
}
// Масса группы без учёта груза
@@ -199,7 +199,7 @@ func (sg ShipGroup) FullMass(st *ShipType) float64 {
// Эффективность двигателя -
// равна мощности Двигателей, умноженной на технологический уровень блока Двигателей
func (sg ShipGroup) DriveEffective(st *ShipType) float64 {
return st.Drive * sg.TechLevel(TechDrive)
return st.Drive * sg.TechLevel(TechDrive).F()
}
// Корабли перемещаются за один ход на количество световых лет, равное
@@ -209,27 +209,27 @@ func (sg ShipGroup) Speed(st *ShipType) float64 {
}
func (sg ShipGroup) UpgradeDriveCost(st *ShipType, drive float64) float64 {
return (1 - sg.TechLevel(TechDrive)/drive) * 10 * st.Drive
return (1 - sg.TechLevel(TechDrive).F()/drive) * 10 * st.Drive
}
// TODO: test on other values
func (sg ShipGroup) UpgradeWeaponsCost(st *ShipType, weapons float64) float64 {
return (1 - sg.TechLevel(TechWeapons)/weapons) * 10 * st.WeaponsBlockMass()
return (1 - sg.TechLevel(TechWeapons).F()/weapons) * 10 * st.WeaponsBlockMass()
}
func (sg ShipGroup) UpgradeShieldsCost(st *ShipType, shields float64) float64 {
return (1 - sg.TechLevel(TechShields)/shields) * 10 * st.Shields
return (1 - sg.TechLevel(TechShields).F()/shields) * 10 * st.Shields
}
func (sg ShipGroup) UpgradeCargoCost(st *ShipType, cargo float64) float64 {
return (1 - sg.TechLevel(TechCargo)/cargo) * 10 * st.Cargo
return (1 - sg.TechLevel(TechCargo).F()/cargo) * 10 * st.Cargo
}
// Мощность бомбардировки
func (sg ShipGroup) BombingPower(st *ShipType) float64 {
return (math.Sqrt(st.Weapons*sg.TechLevel(TechWeapons))/10. + 1.) *
return (math.Sqrt(st.Weapons*sg.TechLevel(TechWeapons).F())/10. + 1.) *
st.Weapons *
sg.TechLevel(TechWeapons) *
sg.TechLevel(TechWeapons).F() *
float64(st.Armament) *
float64(sg.Number)
}
+5 -5
View File
@@ -228,22 +228,22 @@ func TestShipGroupEqual(t *testing.T) {
right = *left
left.SetTechLevel(game.TechDrive, 1.1)
assert.Equal(t, 1.1, left.TechLevel(game.TechDrive))
assert.Equal(t, 1.1, left.TechLevel(game.TechDrive).F())
assert.False(t, left.Equal(right))
right = *left
left.SetTechLevel(game.TechWeapons, 1.1)
assert.Equal(t, 1.1, left.TechLevel(game.TechWeapons))
assert.Equal(t, 1.1, left.TechLevel(game.TechWeapons).F())
assert.False(t, left.Equal(right))
right = *left
left.SetTechLevel(game.TechShields, 1.1)
assert.Equal(t, 1.1, left.TechLevel(game.TechShields))
assert.Equal(t, 1.1, left.TechLevel(game.TechShields).F())
assert.False(t, left.Equal(right))
right = *left
left.SetTechLevel(game.TechCargo, 1.1)
assert.Equal(t, 1.1, left.TechLevel(game.TechCargo))
assert.Equal(t, 1.1, left.TechLevel(game.TechCargo).F())
assert.False(t, left.Equal(right))
// non-essential properties
@@ -254,6 +254,6 @@ func TestShipGroupEqual(t *testing.T) {
// dirty hack to equalize loads
left.Number = 5
left.Load = right.Load / float64(right.Number) * float64(left.Number)
left.Load = game.F(right.Load.F() / float64(right.Number) * float64(left.Number))
assert.True(t, left.Equal(right))
}
+6 -6
View File
@@ -32,16 +32,16 @@ func BlockUpgradeCost(blockMass, currentBlockTech, targetBlockTech float64) floa
func GroupUpgradeCost(sg ShipGroup, st ShipType, drive, weapons, shields, cargo float64) UpgradeCalc {
uc := &UpgradeCalc{Cost: make(map[Tech]float64)}
if drive > 0 {
uc.Cost[TechDrive] = BlockUpgradeCost(st.DriveBlockMass(), sg.TechLevel(TechDrive), drive)
uc.Cost[TechDrive] = BlockUpgradeCost(st.DriveBlockMass(), sg.TechLevel(TechDrive).F(), drive)
}
if weapons > 0 {
uc.Cost[TechWeapons] = BlockUpgradeCost(st.WeaponsBlockMass(), sg.TechLevel(TechWeapons), weapons)
uc.Cost[TechWeapons] = BlockUpgradeCost(st.WeaponsBlockMass(), sg.TechLevel(TechWeapons).F(), weapons)
}
if shields > 0 {
uc.Cost[TechShields] = BlockUpgradeCost(st.ShieldsBlockMass(), sg.TechLevel(TechShields), shields)
uc.Cost[TechShields] = BlockUpgradeCost(st.ShieldsBlockMass(), sg.TechLevel(TechShields).F(), shields)
}
if cargo > 0 {
uc.Cost[TechCargo] = BlockUpgradeCost(st.CargoBlockMass(), sg.TechLevel(TechCargo), cargo)
uc.Cost[TechCargo] = BlockUpgradeCost(st.CargoBlockMass(), sg.TechLevel(TechCargo).F(), cargo)
}
return *uc
}
@@ -69,7 +69,7 @@ func FutureUpgradeLevel(raceLevel, groupLevel, limit float64) float64 {
}
func UpgradeGroupPreference(sg ShipGroup, st ShipType, tech Tech, v float64) ShipGroup {
if v <= 0 || st.BlockMass(tech) == 0 || sg.TechLevel(tech) >= v {
if v <= 0 || st.BlockMass(tech) == 0 || sg.TechLevel(tech).F() >= v {
return sg
}
var su InUpgrade
@@ -84,7 +84,7 @@ func UpgradeGroupPreference(sg ShipGroup, st ShipType, tech Tech, v float64) Shi
ti = len(su.UpgradeTech) - 1
}
su.UpgradeTech[ti].Level = v
su.UpgradeTech[ti].Cost = BlockUpgradeCost(st.BlockMass(tech), sg.TechLevel(tech), v) * float64(sg.Number)
su.UpgradeTech[ti].Cost = BlockUpgradeCost(st.BlockMass(tech), sg.TechLevel(tech).F(), v) * float64(sg.Number)
sg.StateUpgrade = &su
return sg
+41 -21
View File
@@ -7,25 +7,25 @@ import (
)
type UnidentifiedPlanet struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Number uint `json:"number"`
X Float `json:"x"`
Y Float `json:"y"`
Number uint `json:"number"`
}
type UninhabitedPlanet struct {
UnidentifiedPlanet
Size float64 `json:"size"`
Name string `json:"name"`
Resources float64 `json:"resources"` // R - Ресурсы
Capital float64 `json:"capital"` // CAP $ - Запасы промышленности
Material float64 `json:"material"` // MAT M - Запасы ресурсов / сырьё
Size Float `json:"size"`
Name string `json:"name"`
Resources Float `json:"resources"` // R - Ресурсы
Capital Float `json:"capital"` // CAP $ - Запасы промышленности
Material Float `json:"material"` // MAT M - Запасы ресурсов / сырьё
}
type PlanetReport struct {
UninhabitedPlanet
Industry float64 `json:"industry"` // I - Промышленность
Population float64 `json:"population"` // P - Население
Colonists float64 `json:"colonists"` // COL C - Количество колонистов
Industry Float `json:"industry"` // I - Промышленность
Population Float `json:"population"` // P - Население
Colonists Float `json:"colonists"` // COL C - Количество колонистов
Production Production `json:"production"` // TODO: internal/report format
// Параметр "L" - Свободный производственный потенциал
}
@@ -41,13 +41,33 @@ type PlanetReportForeign struct {
PlanetReport
}
func (p *Planet) Mat(v float64) {
p.Material = F(v)
}
func (p *Planet) Pop(v float64) {
p.Population = F(v)
}
func (p *Planet) Col(v float64) {
p.Colonists = F(v)
}
func (p *Planet) Ind(v float64) {
p.Industry = F(v)
}
func (p *Planet) Cap(v float64) {
p.Capital = F(v)
}
func (p Planet) Votes() float64 {
return p.Population / 1000.
return p.Population.F() / 1000.
}
// Производственный потенциал
func (p Planet) ProductionCapacity() float64 {
return PlanetProduction(p.Industry, p.Population)
return PlanetProduction(p.Industry.F(), p.Population.F())
}
func PlanetProduction(industry, population float64) float64 {
@@ -64,12 +84,12 @@ func (p *Planet) ProduceIndustry() {
production := p.ProductionCapacity()
var ind float64
if p.Material > 0 {
ind = math.Min(production/5, p.Material)
p.Material -= ind
ind = math.Min(production/5, p.Material.F())
p.Mat(p.Material.F() - ind)
production -= ind * 5.
}
ind += (production * p.Resources) / (5.*p.Resources + 1.)
p.Industry += ind
ind += (production * p.Resources.F()) / (5.*p.Resources.F() + 1.)
p.Ind(p.Industry.F() + ind)
if p.Industry > p.Population {
p.Capital += p.Industry - p.Population
p.Industry = p.Population
@@ -78,7 +98,7 @@ func (p *Planet) ProduceIndustry() {
// Производство материалов
func (p *Planet) ProduceMaterial() {
p.Material += p.ProductionCapacity() * p.Resources
p.Material = p.Material.Add(p.ProductionCapacity() * p.Resources.F())
}
// Автоматическое увеличение населения на каждом ходу
@@ -115,10 +135,10 @@ func (p *Planet) UnpackColonists() {
}
func UnloadColonists(p Planet, v float64) Planet {
p.Population += v * 8
p.Pop(p.Population.F() + v*8)
if p.Population > p.Size {
p.Colonists += (p.Population - p.Size) / 8
p.Population = p.Size
p.Col(p.Colonists.F() + (p.Population.F()-p.Size.F())/8.)
p.Pop(p.Size.F())
}
return p
}
+35 -35
View File
@@ -36,24 +36,24 @@ func TestProduceIndustry(t *testing.T) {
},
}
HW.ProduceIndustry()
assert.InDelta(t, 196.078, HW.Capital, 0.0005)
assert.InDelta(t, 196.078, HW.Capital.F(), 0.0005)
HW.Capital = 0
HW.Material = 200
HW.ProduceIndustry()
assert.Equal(t, 200., HW.Capital)
assert.Equal(t, 0., HW.Material)
assert.Equal(t, 200., HW.Capital.F())
assert.Equal(t, 0., HW.Material.F())
DW.ProduceIndustry()
assert.InDelta(t, 98.039, DW.Capital, 0.0003)
assert.InDelta(t, 98.039, DW.Capital.F(), 0.0003)
DW.Capital = 0
DW.Material = 100
DW.ProduceIndustry()
assert.Equal(t, 100., DW.Capital)
assert.Equal(t, 0., DW.Material)
assert.Equal(t, 100., DW.Capital.F())
assert.Equal(t, 0., DW.Material.F())
}
func TestProduceMaterial(t *testing.T) {
@@ -67,19 +67,19 @@ func TestProduceMaterial(t *testing.T) {
Industry: 1000,
},
}
assert.Equal(t, 0., HW.Material)
assert.Equal(t, 0., HW.Material.F())
HW.ProduceMaterial()
assert.Equal(t, 10000., HW.Material)
assert.Equal(t, 10000., HW.Material.F())
HW.Industry = 500
HW.Population = 500
HW.ProduceMaterial()
assert.Equal(t, 15000., HW.Material)
assert.Equal(t, 15000., HW.Material.F())
HW.Population = 1000
HW.ProduceMaterial()
assert.Equal(t, 21250., HW.Material)
assert.Equal(t, 21250., HW.Material.F())
}
func TestUnpackCapital(t *testing.T) {
@@ -93,28 +93,28 @@ func TestUnpackCapital(t *testing.T) {
Industry: 1000,
},
}
assert.Equal(t, 0., HW.Capital)
assert.Equal(t, 0., HW.Capital.F())
HW.UnpackCapital()
assert.Equal(t, 1000., HW.Industry)
assert.Equal(t, 0., HW.Capital)
assert.Equal(t, 1000., HW.Industry.F())
assert.Equal(t, 0., HW.Capital.F())
HW.Capital = 123.
HW.UnpackCapital()
assert.Equal(t, 1000., HW.Industry)
assert.Equal(t, 123., HW.Capital)
assert.Equal(t, 1000., HW.Industry.F())
assert.Equal(t, 123., HW.Capital.F())
HW.Industry = 987.
HW.UnpackCapital()
assert.Equal(t, 1000., HW.Industry)
assert.Equal(t, 110., HW.Capital)
assert.Equal(t, 1000., HW.Industry.F())
assert.Equal(t, 110., HW.Capital.F())
HW.Population = 876.
HW.Industry = 800.
HW.UnpackCapital()
assert.Equal(t, 876., HW.Population)
assert.Equal(t, 876., HW.Industry)
assert.Equal(t, 34., HW.Capital)
assert.Equal(t, 876., HW.Population.F())
assert.Equal(t, 876., HW.Industry.F())
assert.Equal(t, 34., HW.Capital.F())
}
func TestUnpackColonists(t *testing.T) {
@@ -128,26 +128,26 @@ func TestUnpackColonists(t *testing.T) {
Industry: 1000,
},
}
assert.Equal(t, 0., HW.Colonists)
assert.Equal(t, 0., HW.Colonists.F())
HW.UnpackColonists()
assert.Equal(t, 1000., HW.Population)
assert.Equal(t, 0., HW.Colonists)
assert.Equal(t, 1000., HW.Population.F())
assert.Equal(t, 0., HW.Colonists.F())
HW.Colonists = 1.05
HW.UnpackColonists()
assert.Equal(t, 1000., HW.Population)
assert.Equal(t, 1.05, HW.Colonists)
assert.Equal(t, 1000., HW.Population.F())
assert.Equal(t, 1.05, HW.Colonists.F())
HW.Population = 996.0
HW.UnpackColonists()
assert.Equal(t, 1000., HW.Population)
assert.Equal(t, 0.55, HW.Colonists)
assert.Equal(t, 1000., HW.Population.F())
assert.Equal(t, 0.55, HW.Colonists.F())
HW.Population = 0.0
HW.UnpackColonists()
assert.Equal(t, 4.4, HW.Population)
assert.Equal(t, 0., HW.Colonists)
assert.Equal(t, 4.4, HW.Population.F())
assert.Equal(t, 0., HW.Colonists.F())
}
func TestProducePopulation(t *testing.T) {
@@ -161,15 +161,15 @@ func TestProducePopulation(t *testing.T) {
Industry: 1000,
},
}
assert.Equal(t, 500., HW.Population)
assert.Equal(t, 0., HW.Colonists)
assert.Equal(t, 500., HW.Population.F())
assert.Equal(t, 0., HW.Colonists.F())
HW.ProducePopulation()
assert.Equal(t, 540., HW.Population)
assert.Equal(t, 0., HW.Colonists)
assert.Equal(t, 540., HW.Population.F())
assert.Equal(t, 0., HW.Colonists.F())
HW.Population = 1000.
HW.ProducePopulation()
assert.Equal(t, 1000., HW.Population)
assert.Equal(t, 10., HW.Colonists)
assert.Equal(t, 1000., HW.Population.F())
assert.Equal(t, 10., HW.Colonists.F())
}