fix: ship production math

This commit is contained in:
IliaDenisov
2026-02-05 21:35:13 +03:00
committed by Ilia Denisov
parent 9088cc77c9
commit 327f2865d4
10 changed files with 158 additions and 205 deletions
+99 -79
View File
@@ -8,6 +8,7 @@ import (
"github.com/iliadenisov/galaxy/internal/controller"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/model/game"
"github.com/iliadenisov/galaxy/internal/number"
"github.com/stretchr/testify/assert"
)
@@ -165,66 +166,22 @@ func TestProduceShips(t *testing.T) {
}
func TestProduceShip(t *testing.T) {
// Drone := game.ShipType{
// Name: "Drone",
// Drive: 1,
// Armament: 0,
// Weapons: 0,
// Shields: 0,
// Cargo: 0,
// }
// BattleShip := game.ShipType{
// Name: "BattleShip",
// Drive: 25,
// Armament: 1,
// Weapons: 30,
// Shields: 35,
// Cargo: 0,
// }
TestDWShip := game.ShipType{
Name: "DROCOLZ",
Drive: 11.32,
Drone := game.ShipType{
Name: "Drone",
Drive: 1,
Armament: 0,
Weapons: 0,
Shields: 0,
Cargo: 1,
Cargo: 0,
}
BattleShip := game.ShipType{
Name: "BattleShip",
Drive: 25,
Armament: 1,
Weapons: 30,
Shields: 35,
Cargo: 0,
}
id := uuid.New()
// p := controller.NewPlanet(0, "Planet_0", &id, 1, 1, 1000, 1000, 1000, 10, game.ProductionShip.AsType(uuid.Nil))
// r := controller.ProduceShip(&p, p.ProductionCapacity(), Drone.EmptyMass())
// assert.Equal(t, uint(99), r)
// assert.InDelta(t, 0.0099, (*p.Production.Progress).F(), 0.000001)
// (&p).Production = game.ProductionShip.AsType(uuid.Nil)
// (&p).Capital = 10.
// r = controller.ProduceShip(&p, p.ProductionCapacity(), Drone.EmptyMass())
// assert.Equal(t, uint(100), r)
// assert.Equal(t, 0., (*p.Production.Progress).F())
// assert.Equal(t, 0., number.Fixed3(p.Capital.F())) // TODO: zero CAP is not actual '0.0' after series of decrements
// (&p).Production = game.ProductionShip.AsType(uuid.Nil)
// (&p).Capital = 0.
// r = controller.ProduceShip(&p, p.ProductionCapacity(), BattleShip.EmptyMass())
// assert.Equal(t, uint(1), r)
// assert.InDelta(t, 0.1, (*p.Production.Progress).F(), 0.001)
// (&p).Production = game.ProductionShip.AsType(uuid.Nil)
// (&p).Capital = 20.
// r = controller.ProduceShip(&p, p.ProductionCapacity(), BattleShip.EmptyMass())
// assert.Equal(t, uint(1), r)
// assert.Equal(t, number.Fixed12(1./9.), (*p.Production.Progress).F())
// test "P" for report
dw := controller.NewPlanet(0, "DW_Planet", &id, 1, 1, 500, 500, 500, 10, game.ProductionShip.AsType(uuid.Nil))
(&dw).Material = 0.32
r := controller.ProduceShip(&dw, dw.ProductionCapacity(), TestDWShip.EmptyMass())
assert.Equal(t, uint(4), r)
assert.Equal(t, 2.30, (*dw.Production.Progress).F())
// TODO: test with insufficient production capacity
}
func TestProduceShip_Research(t *testing.T) {
TestShipCargo1 := game.ShipType{
Name: "Cargo1",
Drive: 30.18,
@@ -241,40 +198,103 @@ func TestProduceShip_Research(t *testing.T) {
Shields: 0,
Cargo: 1,
}
_ = TestShipDROCOLZ
_ = TestShipCargo1
TestShipElephant := game.ShipType{
Name: "ElEphant",
Drive: 80,
Armament: 30,
Weapons: 50,
Shields: 100,
Cargo: 0,
}
id := uuid.New()
var r uint
hw := controller.NewPlanet(0, "Planet_0", &id, 1, 1, 1000, 1000, 1000, 10, game.ProductionShip.AsType(uuid.Nil))
//
// documented data
//
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), Drone.EmptyMass())
assert.Equal(t, uint(99), r)
assert.InDelta(t, 0.0099, (*hw.Production.Progress).F(), 0.000001)
assert.Equal(t, 0.009900990099, (*hw.Production.Progress).F()) // 0.0099 % = 99.0099 mass production per turn
(&hw).Production = game.ProductionShip.AsType(uuid.Nil)
(&hw).Material = 100. // no material deficit
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), Drone.EmptyMass())
assert.Equal(t, uint(100), r)
assert.Equal(t, 0., (*hw.Production.Progress).F())
assert.Equal(t, 0., hw.Material.F())
(&hw).Production = game.ProductionShip.AsType(uuid.Nil)
(&hw).Material = 0.
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), BattleShip.EmptyMass())
assert.Equal(t, uint(1), r)
assert.InDelta(t, 0.1, (*hw.Production.Progress).F(), 0.001)
(&hw).Production = game.ProductionShip.AsType(uuid.Nil)
(&hw).Material = 900. // no material deficit
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), BattleShip.EmptyMass())
assert.Equal(t, uint(1), r)
assert.Equal(t, number.Fixed12(1./9.), (*hw.Production.Progress).F())
//
// real report data
//
dw1 := controller.NewPlanet(0, "DW2", &id, 1, 1, 500, 500, 500, 10, game.ProductionShip.AsType(uuid.Nil))
dw2 := controller.NewPlanet(0, "DW1", &id, 1, 1, 500, 500, 500, 10, game.ProductionShip.AsType(uuid.Nil))
_ = dw1
// (&dw1).Material = 0.0
// r := controller.ProduceShip(&dw1, dw1.ProductionCapacity(), TestShipDROCOLZ.EmptyMass())
// assert.Equal(t, uint(4), r)
// assert.Equal(t, 2.272, (*dw1.Production.FreeProd).F())
(&dw1).Material = 0.0
r = controller.ProduceShip(&dw1, dw1.ProductionCapacity(), TestShipDROCOLZ.EmptyMass())
assert.Equal(t, uint(4), r)
assert.Equal(t, 2.272, (*dw1.Production.ProdUsed).F())
// --------
(&dw2).Material = 0.0
r = controller.ProduceShip(&dw2, dw2.ProductionCapacity(), TestShipCargo1.EmptyMass())
assert.Equal(t, uint(1), r)
assert.Equal(t, 3.282, (*dw2.Production.ProdUsed).F())
var r uint
// (&dw2).Material = 0.0
// r = controller.ProduceShip(&dw2, dw2.ProductionCapacity(), TestShipCargo1.EmptyMass())
// assert.Equal(t, uint(1), r)
// assert.Equal(t, 3.282, (*dw2.Production.FreeProd).F())
// // production stopped and extra MAT released
// matPerShip := controller.ShipMaterialCost(TestShipCargo1.EmptyMass(), float64(dw2.Resources))
// assert.Equal(t, 4.918, matPerShip)
// (&dw2).Material = game.F(controller.ShipProductionCost(matPerShip * float64(*dw2.Production.Progress)))
// assert.Equal(t, 0.32495049505, (&dw2).Material.F()) // old repoert: 0.32
// production stopped and extra MAT released
(&dw2).ReleaseMaterial(TestShipCargo1.EmptyMass())
assert.Equal(t, 0.32495049505, (&dw2).Material.F()) // from report: 0.32
// building new ship with extra MAT
(&dw2).Material = game.F(0.32495049505)
r = controller.ProduceShip(&dw2, dw2.ProductionCapacity(), TestShipDROCOLZ.EmptyMass())
assert.Equal(t, uint(4), r)
assert.Equal(t, 2.3, (*dw2.Production.FreeProd).F())
assert.Equal(t, 2.304495049505, (*dw2.Production.ProdUsed).F()) // from report: 2.3
// TODO: test with insufficient production capacity
//
// insufficient production capacity to produce single ship at one turn
//
assert.Greater(t, TestShipElephant.EmptyMass(), 100.)
// one turn
(&hw).Production = game.ProductionShip.AsType(uuid.Nil)
(&hw).Material = 0.
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), TestShipElephant.EmptyMass())
assert.Equal(t, uint(0), r)
assert.Equal(t, 0., (&hw).Material.F())
assert.InDelta(t, 0.1, (*hw.Production.Progress).F(), 0.01)
(&hw).ReleaseMaterial(TestShipElephant.EmptyMass())
assert.Equal(t, 99.009900990099, (&hw).Material.F())
// two turns
(&hw).Production = game.ProductionShip.AsType(uuid.Nil)
(&hw).Material = 0.
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), TestShipElephant.EmptyMass())
assert.Equal(t, uint(0), r)
assert.Equal(t, 0., (&hw).Material.F())
assert.InDelta(t, 0.1, (*hw.Production.Progress).F(), 0.01)
r = controller.ProduceShip(&hw, hw.ProductionCapacity(), TestShipElephant.EmptyMass())
assert.Equal(t, uint(0), r)
assert.Equal(t, 0., (&hw).Material.F())
assert.InDelta(t, 0.2, (*hw.Production.Progress).F(), 0.01)
(&hw).ReleaseMaterial(TestShipElephant.EmptyMass())
assert.Equal(t, 198.019801980198, (&hw).Material.F())
}
func TestListProducingPlanets(t *testing.T) {