cmd: join ship groups
This commit is contained in:
+18
-73
@@ -1,12 +1,12 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/pkg/error"
|
||||
"github.com/iliadenisov/galaxy/pkg/number"
|
||||
)
|
||||
|
||||
type ShipTypeReport struct {
|
||||
@@ -28,19 +28,8 @@ type ShipTypeReportForeign struct {
|
||||
ShipTypeReport
|
||||
}
|
||||
|
||||
type ShipGroup struct {
|
||||
TypeID uuid.UUID `json:"id"`
|
||||
Type ShipType `json:"-"` // TODO: fill upon load from store
|
||||
Number uint `json:"number"`
|
||||
State string `json:"state"` // TODO: kinda enum: In_Orbit, In_Space, Transfer_State, Upgrade
|
||||
Load float64 `json:"load"` // Cargo loaded - "Масса груза"
|
||||
Drive float64 `json:"drive"`
|
||||
Weapons float64 `json:"weapons"`
|
||||
Shields float64 `json:"shields"`
|
||||
Cargo float64 `json:"cargo"`
|
||||
}
|
||||
|
||||
type Fleet struct {
|
||||
OwnerID uuid.UUID `json:"ownerId"`
|
||||
ShipGroups []ShipGroup `json:"group"`
|
||||
}
|
||||
|
||||
@@ -71,71 +60,27 @@ func (st ShipType) ProductionCost() (mat float64, pop float64) {
|
||||
return
|
||||
}
|
||||
|
||||
// Грузоподъёмность
|
||||
func (sg ShipGroup) CargoCapacity() float64 {
|
||||
return sg.Drive * (sg.Type.Cargo + (sg.Type.Cargo*sg.Type.Cargo)/20)
|
||||
}
|
||||
|
||||
// "Масса перевозимого груза"
|
||||
func (sg ShipGroup) CarryingMass() float64 {
|
||||
return sg.Load / sg.Cargo
|
||||
}
|
||||
|
||||
func (sg ShipGroup) FullMass() float64 {
|
||||
return sg.Type.EmptyMass() + sg.CarryingMass()
|
||||
}
|
||||
|
||||
// "Эффективность двигателя"
|
||||
// равна мощности Двигателей умноженной на текущий технологический уровень блока Двигателей
|
||||
func (sg ShipGroup) DriveEffective() float64 {
|
||||
return sg.Type.Drive * sg.Drive
|
||||
}
|
||||
|
||||
// TODO: test this
|
||||
func (sg ShipGroup) Speed() float64 {
|
||||
return sg.DriveEffective() * 20 / sg.FullMass()
|
||||
}
|
||||
|
||||
func (sg ShipGroup) UpgradeDriveCost(drive float64) float64 {
|
||||
return (1 - sg.Drive/drive) * 10 * sg.Type.Drive
|
||||
}
|
||||
|
||||
// TODO: test on other values
|
||||
func (sg ShipGroup) UpgradeWeaponsCost(weapons float64) float64 {
|
||||
return (1 - sg.Weapons/weapons) * 10 * sg.Type.WeaponsMass()
|
||||
}
|
||||
|
||||
func (sg ShipGroup) UpgradeShieldsCost(shields float64) float64 {
|
||||
return (1 - sg.Shields/shields) * 10 * sg.Type.Shields
|
||||
}
|
||||
|
||||
func (sg ShipGroup) UpgradeCargoCost(cargo float64) float64 {
|
||||
return (1 - sg.Cargo/cargo) * 10 * sg.Type.Cargo
|
||||
}
|
||||
|
||||
// Мощность бомбардировки
|
||||
// TODO: maybe rounding must be done only for display?
|
||||
func (sg ShipGroup) BombingPower() float64 {
|
||||
// return math.Sqrt(sg.Type.Weapons * sg.Weapons)
|
||||
result := (math.Sqrt(sg.Type.Weapons*sg.Weapons)/10. + 1.) *
|
||||
sg.Type.Weapons *
|
||||
sg.Weapons *
|
||||
float64(sg.Type.Armament) *
|
||||
float64(sg.Number)
|
||||
return number.Fixed3(result)
|
||||
}
|
||||
|
||||
// TODO: test this
|
||||
func (fl Fleet) Speed() float64 {
|
||||
func (g Game) FleetSpeed(fl *Fleet) float64 {
|
||||
result := math.MaxFloat64
|
||||
for _, sg := range fl.ShipGroups {
|
||||
if sg.Speed() < result {
|
||||
result = sg.Speed()
|
||||
st := g.mustShipType(sg.TypeID)
|
||||
if sg.Speed(st) < result {
|
||||
result = sg.Speed(st)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (g Game) mustShipType(id uuid.UUID) *ShipType {
|
||||
for ri := range g.Race {
|
||||
if st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.ID == id }); st >= 0 {
|
||||
return &g.Race[ri].ShipTypes[st]
|
||||
}
|
||||
}
|
||||
panic(fmt.Sprintf("mustShipType: ShipType not found: %v", id))
|
||||
}
|
||||
|
||||
func (g Game) ShipTypes(raceName string) ([]ShipType, error) {
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
@@ -240,9 +185,9 @@ func (g Game) mergeShipTypeInternal(ri int, name, targetName string) error {
|
||||
}
|
||||
|
||||
// switch ship groups to the new type
|
||||
for sg := range g.Race[ri].ShipGroups {
|
||||
if g.Race[ri].ShipGroups[sg].TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
g.Race[ri].ShipGroups[sg].TypeID = g.Race[ri].ShipTypes[tt].ID
|
||||
for sg := range g.ShipGroups {
|
||||
if g.ShipGroups[sg].OwnerID == g.Race[ri].ID && g.ShipGroups[sg].TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
g.ShipGroups[sg].TypeID = g.Race[ri].ShipTypes[tt].ID
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user