cmd: disassebmle group

This commit is contained in:
Ilia Denisov
2025-12-25 21:32:19 +03:00
parent d0d5bc3c88
commit 6157c07a35
4 changed files with 194 additions and 19 deletions
+81 -4
View File
@@ -81,10 +81,15 @@ func (sg ShipGroup) CarryingMass() float64 {
return sg.Load / sg.Cargo
}
// Полная масса -
// массу корабля самого по себе плюс масса перевозимого груза.
// Масса группы без учёта груза
func (sg ShipGroup) EmptyMass(st *ShipType) float64 {
return st.EmptyMass() * float64(sg.Number)
}
// Полная масса -
// массу корабля самого по себе плюс масса перевозимого груза
func (sg ShipGroup) FullMass(st *ShipType) float64 {
return st.EmptyMass() + sg.CarryingMass()
return sg.EmptyMass(st) + sg.CarryingMass()
}
// Эффективность двигателя -
@@ -94,7 +99,7 @@ func (sg ShipGroup) DriveEffective(st *ShipType) float64 {
}
// Корабли перемещаются за один ход на количество световых лет, равное
// эффективности двигателя, умноженной на 20 и деленной на "Полную массу" корабля.
// эффективности двигателя, умноженной на 20 и деленной на "Полную массу" корабля
func (sg ShipGroup) Speed(st *ShipType) float64 {
return sg.DriveEffective(st) * 20 / sg.FullMass(st)
}
@@ -145,6 +150,78 @@ func (g *Game) BreakGroup(raceName string, groupIndex, quantity uint) error {
return g.breakGroupInternal(ri, groupIndex, quantity)
}
func (g *Game) DisassembleGroup(raceName string, groupIndex, quantity uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
}
return g.disassembleGroupInternal(ri, groupIndex, quantity)
}
func (g *Game) disassembleGroupInternal(ri int, groupIndex, quantity uint) error {
sgi := -1
var maxIndex uint
for i, sg := range g.listIndexShipGroups(ri) {
if sgi < 0 && sg.Index == groupIndex {
sgi = i
}
if sg.Index > maxIndex {
maxIndex = sg.Index
}
}
if sgi < 0 {
return e.NewEntityNotExistsError("group #%d", groupIndex)
}
if g.ShipGroups[sgi].State != "In_Orbit" || g.ShipGroups[sgi].Origin != nil || g.ShipGroups[sgi].Range != nil {
return e.NewShipsBusyError()
}
if g.ShipGroups[sgi].Number < quantity {
return e.NewBeakGroupNumberNotEnoughError("%d<%d", g.ShipGroups[sgi].Number, quantity)
}
pl := slices.IndexFunc(g.Map.Planet, func(p Planet) bool { return p.Number == g.ShipGroups[sgi].Destination })
if pl < 0 {
return e.NewGameStateError("planet #%d", g.ShipGroups[sgi].Destination)
}
var sti int
if sti = slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.ID == g.ShipGroups[sgi].TypeID }); sti < 0 {
// hard to test, need manual game data invalidation
return e.NewGameStateError("not found: ShipType ID=%v", g.ShipGroups[sgi].TypeID)
}
if quantity > 0 && quantity < g.ShipGroups[sgi].Number {
// make new group for disassembly
nsgi, err := g.breakGroupSafe(ri, groupIndex, quantity)
if err != nil {
return err
}
sgi = nsgi
}
if g.ShipGroups[sgi].CargoType != nil {
ct := *g.ShipGroups[sgi].CargoType
load := g.ShipGroups[sgi].Load
switch ct {
case CargoColonist:
if g.Map.Planet[pl].Owner == g.Race[ri].ID {
g.Map.Planet[pl] = UnloadColonists(g.Map.Planet[pl], load)
}
case CargoMaterial:
g.Map.Planet[pl].Material += load
case CargoCapital:
g.Map.Planet[pl].Capital += load
}
}
g.Map.Planet[pl].Material += g.ShipGroups[sgi].EmptyMass(&g.Race[ri].ShipTypes[sti])
g.ShipGroups = append(g.ShipGroups[:sgi], g.ShipGroups[sgi+1:]...)
return nil
}
func (g *Game) LoadCargo(raceName string, groupIndex uint, cargoType string, ships uint, quantity float64) error {
ri, err := g.raceIndex(raceName)
if err != nil {