ui calculator
This commit is contained in:
@@ -3,10 +3,10 @@ package controller
|
||||
import (
|
||||
"iter"
|
||||
"maps"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"slices"
|
||||
|
||||
"galaxy/calc"
|
||||
"galaxy/game/internal/model/game"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -65,7 +65,7 @@ func FilterBattleOpponents(c *Cache, attIdx, defIdx int, cacheProbability map[in
|
||||
return true
|
||||
}
|
||||
|
||||
p := DestructionProbability(
|
||||
p := calc.DestructionProbability(
|
||||
c.ShipGroupShipClass(attIdx).Weapons.F(),
|
||||
c.ShipGroup(attIdx).TechLevel(game.TechWeapons).F(),
|
||||
c.ShipGroupShipClass(defIdx).Shields.F(),
|
||||
@@ -216,16 +216,6 @@ func SingleBattle(c *Cache, b *Battle) {
|
||||
}
|
||||
}
|
||||
|
||||
func DestructionProbability(attWeapons, attWeaponsTech, defShields, defShiledsTech, defFullMass float64) float64 {
|
||||
effAttack := attWeapons * attWeaponsTech
|
||||
effDefence := EffectiveDefence(defShields, defShiledsTech, defFullMass)
|
||||
return (math.Log10(effAttack/effDefence)/math.Log10(4) + 1) / 2
|
||||
}
|
||||
|
||||
func EffectiveDefence(defShields, defShiledsTech, defFullMass float64) float64 {
|
||||
return defShields * defShiledsTech / math.Pow(defFullMass, 1./3.) * math.Pow(30., 1./3.)
|
||||
}
|
||||
|
||||
func randomValue(v iter.Seq[int]) int {
|
||||
ids := slices.Collect(v)
|
||||
return ids[rand.IntN(len(ids))]
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"galaxy/calc"
|
||||
"galaxy/game/internal/controller"
|
||||
"galaxy/game/internal/model/game"
|
||||
|
||||
@@ -39,25 +40,25 @@ var (
|
||||
)
|
||||
|
||||
func TestDestructionProbability(t *testing.T) {
|
||||
probability := controller.DestructionProbability(ship.Weapons.F(), 1, ship.Shields.F(), 1, ship.EmptyMass())
|
||||
probability := calc.DestructionProbability(ship.Weapons.F(), 1, ship.Shields.F(), 1, ship.EmptyMass())
|
||||
assert.Equal(t, .5, probability)
|
||||
|
||||
undefeatedShip := ship
|
||||
undefeatedShip.Shields = 55
|
||||
probability = controller.DestructionProbability(ship.Weapons.F(), 1, undefeatedShip.Shields.F(), 1, undefeatedShip.EmptyMass())
|
||||
probability = calc.DestructionProbability(ship.Weapons.F(), 1, undefeatedShip.Shields.F(), 1, undefeatedShip.EmptyMass())
|
||||
assert.LessOrEqual(t, probability, 0.)
|
||||
|
||||
disruptiveShip := ship
|
||||
disruptiveShip.Weapons = 40
|
||||
probability = controller.DestructionProbability(disruptiveShip.Weapons.F(), 1, ship.Shields.F(), 1, ship.EmptyMass())
|
||||
probability = calc.DestructionProbability(disruptiveShip.Weapons.F(), 1, ship.Shields.F(), 1, ship.EmptyMass())
|
||||
assert.GreaterOrEqual(t, probability, 1.)
|
||||
}
|
||||
|
||||
func TestEffectiveDefence(t *testing.T) {
|
||||
assert.Equal(t, 10., controller.EffectiveDefence(ship.Shields.F(), 1, ship.EmptyMass()))
|
||||
assert.Equal(t, 10., calc.EffectiveDefence(ship.Shields.F(), 1, ship.EmptyMass()))
|
||||
|
||||
attackerEffectiveDefence := controller.EffectiveDefence(attacker.Shields.F(), 1, attacker.EmptyMass())
|
||||
defenderEffectiveDefence := controller.EffectiveDefence(defender.Shields.F(), 1, defender.EmptyMass())
|
||||
attackerEffectiveDefence := calc.EffectiveDefence(attacker.Shields.F(), 1, attacker.EmptyMass())
|
||||
defenderEffectiveDefence := calc.EffectiveDefence(defender.Shields.F(), 1, defender.EmptyMass())
|
||||
|
||||
// attacker's effective shields must be 'just' 4 times greater than defender's
|
||||
assert.InDelta(t, defenderEffectiveDefence*4, attackerEffectiveDefence, 0)
|
||||
@@ -123,7 +124,7 @@ func TestFilterBattleOpponents(t *testing.T) {
|
||||
assert.True(t, controller.FilterBattleOpponents(c, 2, 0, cacheProbability))
|
||||
assert.NoError(t, c.UpdateRelation(Race_0_idx, Race_1_idx, game.RelationWar))
|
||||
|
||||
assert.LessOrEqual(t, controller.DestructionProbability(Cruiser.Weapons.F(), 1, undefeatedShip.Shields.F(), 1, undefeatedShip.EmptyMass()), 0.)
|
||||
assert.LessOrEqual(t, calc.DestructionProbability(Cruiser.Weapons.F(), 1, undefeatedShip.Shields.F(), 1, undefeatedShip.EmptyMass()), 0.)
|
||||
assert.True(t, controller.FilterBattleOpponents(c, 1, 3, cacheProbability))
|
||||
assert.NotContains(t, cacheProbability[1], 3)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"iter"
|
||||
"slices"
|
||||
|
||||
"galaxy/calc"
|
||||
"galaxy/util"
|
||||
|
||||
e "galaxy/error"
|
||||
@@ -272,7 +273,7 @@ func ProduceShip(p *game.Planet, productionAvailable, shipMass float64) uint {
|
||||
}
|
||||
ships := uint(0)
|
||||
pa := productionAvailable
|
||||
PRODcost := ShipProductionCost(shipMass)
|
||||
PRODcost := calc.ShipProductionCost(shipMass)
|
||||
var MATneed, MATfarm, totalCost float64
|
||||
for {
|
||||
MATneed = shipMass - float64(p.Material)
|
||||
@@ -281,8 +282,6 @@ func ProduceShip(p *game.Planet, productionAvailable, shipMass float64) uint {
|
||||
}
|
||||
MATfarm = MATneed / float64(p.Resources)
|
||||
totalCost = PRODcost + MATfarm
|
||||
// fmt.Printf("PRODcost: %3.03f MATcost: %3.03f MAThave: %3.03f MATneed: %3.03f MATfarm: %3.03f total: %3.03f \n",
|
||||
// PRODcost, shipMass, float64(p.Material), MATneed, MATfarm, totalCost)
|
||||
if pa < totalCost {
|
||||
progress := pa / totalCost
|
||||
pval := game.F(progress)
|
||||
@@ -292,7 +291,6 @@ func ProduceShip(p *game.Planet, productionAvailable, shipMass float64) uint {
|
||||
p.Production.Progress = &pval
|
||||
fval := game.F(pa)
|
||||
p.Production.ProdUsed = &fval
|
||||
// fmt.Println("pa", pa, "progress", progress, "MAT:", progress*shipMass)
|
||||
return ships
|
||||
} else {
|
||||
pa -= totalCost
|
||||
@@ -301,10 +299,3 @@ func ProduceShip(p *game.Planet, productionAvailable, shipMass float64) uint {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ShipProductionCost(shipMass float64) float64 {
|
||||
return shipMass * 10.
|
||||
}
|
||||
func ShipMaterialCost(shipMass, planetResource float64) float64 {
|
||||
return shipMass / planetResource
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"iter"
|
||||
"slices"
|
||||
|
||||
"galaxy/calc"
|
||||
mr "galaxy/model/report"
|
||||
|
||||
"galaxy/util"
|
||||
@@ -540,7 +541,7 @@ func (c *Cache) ReportShipProduction(ri int, rep *mr.Report) {
|
||||
sliceIndexValidate(&rep.ShipProduction, i)
|
||||
rep.ShipProduction[pi].Planet = p.Number
|
||||
rep.ShipProduction[pi].Class = st.Name
|
||||
rep.ShipProduction[pi].Cost = mr.F(ShipProductionCost(st.EmptyMass()))
|
||||
rep.ShipProduction[pi].Cost = mr.F(calc.ShipProductionCost(st.EmptyMass()))
|
||||
rep.ShipProduction[pi].Free = mr.F(c.PlanetProductionCapacity(p.Number))
|
||||
rep.ShipProduction[pi].ProdUsed = mr.F((*p.Production.ProdUsed).F())
|
||||
rep.ShipProduction[pi].Percent = mr.F((*p.Production.Progress).F())
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"iter"
|
||||
"slices"
|
||||
|
||||
"galaxy/calc"
|
||||
"galaxy/util"
|
||||
|
||||
e "galaxy/error"
|
||||
@@ -16,7 +17,7 @@ import (
|
||||
|
||||
func (c *Cache) ShipClassCreate(ri int, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
|
||||
c.validateRaceIndex(ri)
|
||||
if err := validateShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
|
||||
if err := calc.ValidateShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
|
||||
return err
|
||||
}
|
||||
n, ok := util.ValidateTypeName(typeName)
|
||||
@@ -159,32 +160,3 @@ func (c *Cache) MustShipType(ri int, ID uuid.UUID) *game.ShipType {
|
||||
}
|
||||
panic(fmt.Sprintf("ship class not found: race_idx=%d id=%v", ri, ID))
|
||||
}
|
||||
|
||||
func validateShipTypeValues(d float64, a int, w, s, c float64) error {
|
||||
if !checkShipTypeValueDWSC(d) {
|
||||
return e.NewDriveValueError(d)
|
||||
}
|
||||
if !checkShipTypeValueDWSC(w) {
|
||||
return e.NewWeaponsValueError(w)
|
||||
}
|
||||
if !checkShipTypeValueDWSC(s) {
|
||||
return e.NewShieldsValueError(s)
|
||||
}
|
||||
if !checkShipTypeValueDWSC(c) {
|
||||
return e.NewCargoValueError(s)
|
||||
}
|
||||
if a < 0 {
|
||||
return e.NewShipTypeArmamentValueError(a)
|
||||
}
|
||||
if (w == 0 && a > 0) || (a == 0 && w > 0) {
|
||||
return e.NewShipTypeArmamentAndWeaponsValueError("A=%d W=%.0f", a, w)
|
||||
}
|
||||
if d == 0 && w == 0 && s == 0 && c == 0 && a == 0 {
|
||||
return e.NewShipTypeShipTypeZeroValuesError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkShipTypeValueDWSC(v float64) bool {
|
||||
return v == 0 || v >= 1
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"galaxy/calc"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
@@ -179,18 +180,13 @@ func (sg ShipGroup) Equal(other ShipGroup) bool {
|
||||
sg.State() == other.State()
|
||||
}
|
||||
|
||||
// Грузоподъёмность
|
||||
// Грузоподъёмность группы
|
||||
func (sg ShipGroup) CargoCapacity(st *ShipType) float64 {
|
||||
return sg.TechLevel(TechCargo).F() * (st.Cargo.F() + (st.Cargo.F()*st.Cargo.F())/20) * float64(sg.Number)
|
||||
return calc.CargoCapacity(st.Cargo.F(), sg.TechLevel(TechCargo).F()) * float64(sg.Number)
|
||||
}
|
||||
|
||||
// Масса перевозимого груза -
|
||||
// общее количество единиц груза, деленное на технологический уровень Грузоперевозок
|
||||
func (sg ShipGroup) CarryingMass() float64 {
|
||||
if sg.Load.F() == 0 {
|
||||
return 0
|
||||
}
|
||||
return sg.Load.F() / sg.TechLevel(TechCargo).F()
|
||||
return calc.CarryingMass(sg.Load.F(), sg.TechLevel(TechCargo).F())
|
||||
}
|
||||
|
||||
// Масса группы без учёта груза
|
||||
@@ -198,22 +194,16 @@ func (sg ShipGroup) EmptyMass(st *ShipType) float64 {
|
||||
return st.EmptyMass() * float64(sg.Number)
|
||||
}
|
||||
|
||||
// Полная масса -
|
||||
// массу корабля самого по себе плюс масса перевозимого груза
|
||||
func (sg ShipGroup) FullMass(st *ShipType) float64 {
|
||||
return sg.EmptyMass(st) + sg.CarryingMass()
|
||||
return calc.FullMass(sg.EmptyMass(st), sg.CarryingMass())
|
||||
}
|
||||
|
||||
// Эффективность двигателя -
|
||||
// равна мощности Двигателей, умноженной на технологический уровень блока Двигателей
|
||||
func (sg ShipGroup) DriveEffective(st *ShipType) float64 {
|
||||
return st.Drive.F() * sg.TechLevel(TechDrive).F()
|
||||
return calc.DriveEffective(st.Drive.F(), sg.TechLevel(TechDrive).F())
|
||||
}
|
||||
|
||||
// Корабли перемещаются за один ход на количество световых лет, равное
|
||||
// эффективности двигателя, умноженной на 20 и деленной на "Полную массу" корабля
|
||||
func (sg ShipGroup) Speed(st *ShipType) float64 {
|
||||
return sg.DriveEffective(st) * 20 / sg.FullMass(st)
|
||||
return calc.Speed(sg.DriveEffective(st), sg.FullMass(st))
|
||||
}
|
||||
|
||||
// Мощность бомбардировки
|
||||
|
||||
@@ -2,6 +2,7 @@ package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"galaxy/calc"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -44,10 +45,11 @@ func (st ShipType) DriveBlockMass() float64 {
|
||||
}
|
||||
|
||||
func (st ShipType) WeaponsBlockMass() float64 {
|
||||
if (st.Armament == 0 && st.Weapons != 0) || (st.Armament != 0 && st.Weapons == 0) {
|
||||
if v, ok := calc.WeaponsBlockMass(st.Weapons.F(), st.Armament); !ok {
|
||||
panic(fmt.Sprintf("ship class invalid design: A=%d W=%.03f", st.Armament, st.Weapons))
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
return float64(st.Armament+1) * (st.Weapons.F() / 2)
|
||||
}
|
||||
|
||||
func (st ShipType) ShieldsBlockMass() float64 {
|
||||
@@ -59,6 +61,10 @@ func (st ShipType) CargoBlockMass() float64 {
|
||||
}
|
||||
|
||||
func (st ShipType) EmptyMass() float64 {
|
||||
shipMass := st.DriveBlockMass() + st.ShieldsBlockMass() + st.CargoBlockMass() + st.WeaponsBlockMass()
|
||||
return shipMass
|
||||
if v, ok := calc.EmptyMass(st.Drive.F(), st.Weapons.F(), st.Armament, st.Shields.F(), st.Cargo.F()); !ok {
|
||||
panic(fmt.Sprintf("ship class invalid design: D=%.03f A=%d W=%.03f S=%.03f C=%.03f",
|
||||
st.Drive, st.Armament, st.Weapons, st.Shields, st.Cargo))
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user